Weverton Peron
Weverton Peron

Reputation: 97

Resume Application from a Notification

I have created a notification when user opens an activity, if user put app in background and touch up in notification, app tries to re-open activity, but in fact a new activity is opened and last data is lost.

Can anyone help me?

Upvotes: 0

Views: 384

Answers (2)

Ndivhuwo
Ndivhuwo

Reputation: 287

So for me this worked;

<activity android:name=".YourActivity" android:launchMode="singleTop">

Single task like suggested in the approved answer does work as well, but it's not recommended;

"singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications."

Upvotes: 0

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

You can try making the Activity as a single task by adding android:launchMode="singleTask" to your AndroidManifest.xml. Something like this:

<activity
    android:name=".YourActivity"
    android:launchMode="singleTask">

Then you need to handle the new data via onNewIntent(), something like this:

public class YourActivity extends Activity {

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);
    ...
    processExtraData();
  }

  @Override protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    processExtraData();
  }

  protected void processExtraData() {
    Bundle extras = getIntent().getExtras();

    ...
    // process the extra here.
  }
}

Upvotes: 2

Related Questions