user4702646
user4702646

Reputation:

Android task and back stack. Managing the history

Trying to manage task and back stack in an android application.
I use a sliding menu in it, from which I can start different activities: Activity1, Activity2... (menu is similar in each activity).
MainActivity is a Launcher.
I can navigate between these activities and when I press back button I must always come back to MainActivity.

That's why I am starting activities from menu without history:

Intent i1 = new Intent(getApplicationContext(), Activity1.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i1);

The problem is that from Activity1 I also can start an intent where I have to keep the history:

Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);

After dialing I want come back to Activity1, But as you can see now I am coming back to MainActivity (because of no history).

Also I tried to start new activities with history and finish previous Activity:

  Intent i1 = new Intent(getApplicationContext(), Activity1.class);
  finish();
  startActivity(i1);

Here everything is OK with Dial intent (i will not finish Activity1 before dialing), but whenever i press the back button I quit the application.

Tried to show the idea on the picture

enter image description here

Also tried to play with noHistory in manifest, but have not succeed.
Can I kind of return the the history in (with wlags or extras) before startind the Dial intent from activity, which is without history??

How to solve it?

Upvotes: 0

Views: 662

Answers (2)

SaNtoRiaN
SaNtoRiaN

Reputation: 2202

The app quits because you finish your MainActivity on going to Activity1 so to solve it: you can delete the finish() while starting Activity1, or restart the MainActivity when you click the back button in Activity1 by adding this code to your Activity1

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

Upvotes: 1

Danfoa
Danfoa

Reputation: 920

If your App Map will not change, you could set the Parent Activity attribute in your manifest for every Activity you have.

For example

for Activity 1/2/3 - the parent will be your MainActivity.

You do this by changing your activity item in your manifest with a

<meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />

Another way to do so is overriding the "onBackPressed" method in the activitie class that behave differently

Upvotes: 0

Related Questions