简体   繁体   中英

How to resume activity in Android?

I am trying to create my own Alarm Android application. I want to achieve that when alarm is triggered, MainActivity is resumed ( not created again ). It basically means that if I set alarm and leave my application, I want that application to be resumed when alarm is triggered.

Currently, I am facing a problem that when alarm is triggered while my application runs in background and I click on application icon, onCreate method is called and basically two instances of application are running simultaneously (I have used Toast messages to confirm this). I expected that click on application icon will cause its resuming if it is already running in background, but it seems it is not the case.

Also, I have tried procedure explained here: Resume activity in Android but it didn't work for me, Toast message from onCreate method appears on screen.

Can anybody help, please? I am really running out of ideas here. Thanks in advance!

Solution:

Intent intent1 = new Intent(activity, ResultActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent1.putExtra(Extras.EXTRA_NOTIFICATION_ID, true);
                intent1.putExtra("notificationId", EXPORT_NOTIFICATION_ID);

                Log.i(TAG, "onPostExecute: app is BackGround ");
                pendingIntent = PendingIntent.getActivity(activity, EXPORT_NOTIFICATION_ID, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(pendingIntent);
                mNotifyManager.cancel(EXPORT_NOTIFICATION_ID);


                Intent buttonIntent = new Intent(activity, NotificationReceiver.class);
                buttonIntent.putExtra("notificationId", EXPORT_NOTIFICATION_ID);
                PendingIntent dismissIntent = PendingIntent.getBroadcast(activity, 0, buttonIntent, 0);

                builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent);
                builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent);



    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
    notificationBuilder.setPriority(Notification.PRIORITY_MAX);
    notificationBuilder
            .setSmallIcon(R.drawable.status_icon)
            .setColor(ContextCompat.getColor(mContext, R.color.transparent))
            .setContentIntent(NotificationPendingIntent)
            .setTicker(notificationData.getCleverTapTitle())
            .setAutoCancel(isAutoCancelable())
            .setContentTitle(notificationData.getCleverTapTitle())
            .setContentText(notificationData.getCleverTapMessage())
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND);


    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
    notificationBuilder.setChannelId(NotificationChannelName.GENERAL);

    /*Create a Delete Intent that will be called when user removes the notification by swiping or by clear*/
    notificationBuilder.setDeleteIntent(getNotificationDeletePendingIntent());


    notification = notificationBuilder.build();
    notification.flags |= NotificationCompat.FLAG_AUTO_CANCEL;
    if (BMSUiUtility.isNotificationFromInbox(rawNotificaionData, mSharedPreferencesManager)) {
        mNotificationManager.notify(notificationId, notification);
    } else {
        mNotificationManager.notify(0, notification);
    }

Use the following code to cancel a Notification Your MainActivity onDestroy Mathod call:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

In this code there is alway the same id used for notifications. If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.

AndroidManifest.xml:

<activity
            android:name="com.ui.activity.ResultActivity"
            android:screenOrientation="portrait"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

What you need to do is specify your activity's launch mode to singleTask or singleInstance . To do this, go to your AndroidManifest.xml and change/add launchMode to your activity.

<activity
    android:name=".YourActivity"
    android:label="Your Activity"
    android:launchMode="singleInstance">

More info on the differences of different launch modes are explained here: https://developer.android.com/guide/topics/manifest/activity-element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM