0

I have an application A with an activity that launches another application B as follow:

@Override
protected void onResume() {
    super.onResume();

    if (launched == false) {
        Intent intent = getPackageManager().getLaunchIntentForPackage(apk);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
        launched = true;
    } else {
        // Here I launch another activity of application A
        launched = false;
    }
}

This other application B have a close button to finish its main activity.

The logic behavior is to return on the first application A in the onResume()'s "else". Sometimes the onDestroy() method is called from my "launch" activity of the application A and the Android Desktop appears when application B stops.

My first thought is about the Android memory management. If the application B takes to much memory or if the Android GC turns itself on at this moment, the application A can be killed (Application A takes a lot of memory but it is another problem).

In the manifest, I put android:persistent="true" but it doesn't change anything.

Do you have any idea of how can I be sure to go back to application A when application B is over ?

EDIT: When application B finish, the application A activity where I launched the application B doesn't go to onResume() but directly to onStop(). I thought it was like onStart -> onResume (here I launch application B) -> onPause (application B is now visible) -> onResume (it is the second time in onResume so the application B is over).

Thanks

ldemay
  • 500
  • 7
  • 22

2 Answers2

0

Simply use startActivityForResult instead of startActivity. Be sure you don't lose state of calling activity, you need to save all pertinent data by overriding the onSaveInstanceState and adding data to the Bundle. Also override onRestoreInstanceState to handle when your activity come back and need to restore previous data.

Miroslav Michalec
  • 1,569
  • 1
  • 19
  • 22
  • It doesn't change anything (replacing `startActivity` by `startActivityForResult`). I just implement `onSaveInstanceState`/`onRestoreInstanceState` using the bundle to store my "launched" flag but nothing changes. – ldemay Jan 14 '14 at 16:27
  • 1
    Implement `onActivityResult` method and (or) try to remove Intent.FLAG_ACTIVITY_NO_HISTORY. – Miroslav Michalec Jan 14 '14 at 19:50
  • Indeed, without `FLAG_ACTIVITY_NO_HISTORY`, the Activity Lifecycle is as expected. Thanks! – ldemay Jan 21 '14 at 09:09
0

Returning to calling activity

Android: Go back to previous activity

Both startActivityForResult and startActivity can be used to call another activity. Note that startActivityForResult relies on the called activity to call setResult before it finishes.

Getting back to the activity in the right state

To "be sure", you can get back to application A in its state when you called B, you might have to persist its activity state, so that when application B returns, you can "recreate" it in the right state.

Quick Links:

Saving Android Activity state using Save Instance State

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Community
  • 1
  • 1
Dannie
  • 2,430
  • 14
  • 16
  • You could just try calling own mini test application activity and see if it works. If it does, then it might be that Application B is faulty. – Dannie Jan 14 '14 at 16:41
  • I'll try do do this. Anyway thanks for pointing the link about recreation in activity lifecycle. – ldemay Jan 14 '14 at 16:59