Issue
I have an already visible activity (so onStart has run), I pull down the notifications bar, click on a notification associated with a pending intent for said activity. 1) onPause is called. Note that onStop is NOT called 2) onCreate for a new instance of the activity is called then onStart, etc...
As suggested I've tried singleTask and singleInstance but they don't prevent a new instance being created after onPause is called. Interestingly, this only happens when the activity is visible and I click its notification. If it's already been stopped, Android uses the old instance. Perhaps I need to tweak how the PendingIntent is generated...
Solution
I think it's possible that a new Activity is created in the stack when you run the Pending Intent. Configure your activity so that it does not create a new Activity via launchMode set to singleTask or singleInstance.
Here is an example of setup I have used for an activity:
<activity
android:name="com.zakimak.HomeScreenActivity"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:launchMode="singleTask" />
Then building the pending intent is as follows:
Intent resultIntent = new Intent(context, HomeScreenActivity.class);
// The request code 24601 uniquely identifies the notification generated
// by the application. As an application may generate several having
//different codes to identify each. CAUTION: It's value should be greater than 0
//.i.e. RESULT_FIRST_USER+
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
24601, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title")
.setContentText("Bla bla").setOngoing(true)
.setTicker("Bla bla is running").setAutoCancel(false)
.setContentIntent(resultPendingIntent).build();
Step1: I start the application and show HomeScreenActivity. Then I drag the notification bar and click the notification. It opens the Activity and the following callback methods are called : onPause and then onResume. onPause is called because activity could still be visible under the drawer.
Step2: I start the application and show HomeScreenActivity and press Home or launch another activity, then onStop is called. Then I drag the notification bar and click the notification. It opens the Activity and the following callback methods are called : onStart and then onResume.
In both of the cases onCreate is called only once, when the Activity is launched for the first iteration. It was tested in emulator.
In some cases when your device is in heavy load or trying to save power, it's possible the Android might kill the process.
Answered By - ZakiMak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.