Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Run application from last Activity

I have an application with several Activities. My A Activity has the Manifest Intent filter parameters: action.MAIN and category.LAUNCHER. after its being loaded I call Activity B and finish() A since I don't use it anymore.

After I run my application, go from Activity A to B and press the Home button, when I relaunch it from the applications menu or from the Market app for ex.(not by a long press on the Home button), it starts from the A Activity and do not save its last Activity B.

I definitely know that this is possible to relaunch an application from its last Activity (some application from the Market do support it) and I think that this can be determined by the Manifest parameters but I don't know which one.

does anyone know how to implement it so my application can relaunch from its last Activity B?

Thanks ayanir

like image 327
ayanir Avatar asked Jan 23 '10 09:01

ayanir


2 Answers

Though I know this is an old question, I was struggling with this very issue and couldn't find an answer on SO. So, here is my (very newbie) answer:

No, I do not think it's possible to do this by messing with the manifest - you can only launch one fixed activity per app from the home screen. What you can do, though, is launch whatever activity you want from that starting point, and Android can do it quickly enough that you never see the first one.

Though this feels very much like a hack, I implemented this routing in the starting activity's onResume() method, and used sharedPreferences to keep track of which activity to launch:

    final Class<? extends Activity> activityClass;
    SharedPreferences prefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
    int activityID = prefs.getInt("whichActivity", -1);
    if (activityID  == Constants.ACTIVITY_ID_MAINSCREEN) {
        activityClass = MainScreen.class;
    } else {
        activityClass = null; return;
    }
    Intent newActivity = new Intent(this, activityClass);
    this.startActivity(newActivity);
like image 157
Sam L. Avatar answered Sep 22 '22 19:09

Sam L.


There have been a number of very similar questions lately. It's a good idea to search the site first to ensure that duplicate questions don't get asked.

For example, the question linked below says that this behaviour was happening because the developer was starting their application using the Eclipse debugger. Another person was having this problem because they were launching the application directly from Eclipse, rather than starting cleanly by manually pressing the launcher icon.

Android: keep task's activity stack after restart from HOME

like image 24
Christopher Orr Avatar answered Sep 25 '22 19:09

Christopher Orr