2

I am trying to re-open last activity on icon press. But it starts from splash when I remove launchMode SingleTask from main.

Below is the scenario - A = Splash , B = Activity1 , C = Activity 2.

I want to launch C on icon press.

On Start
A ---->B---->C (Here home icon pressed) On Click Icon It runs A but I want C . Can any one solve this?

Launching C-

Intent urlIntent = new Intent(this,C.class); 
urlIntent.putExtra("stt",Str);
startActivityForResult(urlIntent,REQUEST_CODE);
Kumar Bankesh
  • 296
  • 4
  • 27
  • 1
    https://stackoverflow.com/questions/5757562/how-do-i-show-a-splash-screen-only-when-the-activity-starts-up-not-when-it-is-re Do you require the splash screen? – jL4 Sep 08 '17 at 18:16
  • @jL4 splash isn't mandatory and I cant finish previous activity. – Kumar Bankesh Sep 11 '17 at 05:44
  • 1
    What do you exactly - after put in background when you press your app icon from menu - the last activity will be automatically resumed !! Or do you want something like after closing your app, when restarted from menu, after clicking on your app icon the last activity should be resumed ? – shadygoneinsane Sep 11 '17 at 10:51
  • @shadygoneinsane I have updated the question I am only trying to open second activity where it was. – Kumar Bankesh Sep 12 '17 at 13:06
  • @Banku I have posted an solution. do check and I hope this helps . – shadygoneinsane Sep 13 '17 at 05:29
  • I think you should visit this thread [Android: bug in launchMode=“singleTask”? -> activity stack not preserved](https://stackoverflow.com/questions/2417468/android-bug-in-launchmode-singletask-activity-stack-not-preserved) – Obito Sep 14 '17 at 07:14
  • 1
    Please post your manifest. You may be seeing a nasty long-standing Android bug that has never been fixed. See https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508#16447508 for more details and a workaround. To check if you are seeing this bug, install your app on the phone. Kill the app (settings->apps->yourapp->force close). Now start your app from the HOME screen icon. Open ActivityB -> ActivityC. Press HOME. launch your app again by pressing the app icon on the HOME screen. If you see ActivityC you're seeing this nasty bug. – David Wasser Sep 15 '17 at 14:03

2 Answers2

3

CASE I. Check if your app is in background after pressing the home button. If it is then it should open activity C.

CASE II. If your app is killed due to low on memory then it will start from launcher activity i.e. activity A.

I also faced this issue. This issue occurred due to changed setting of device.If user enable "Don't keep activities" then all activities will be destroyed if you press home or start new activity.

Note: I used the default launch mode

 Attaching a screen for reference

user320676
  • 394
  • 2
  • 19
0

Your app will always start from the Activity with the filter intent.action.MAIN in the Manifest.xml.

And if your application is already running then the next time time your app starts it will automatically resume from the last opened activity.

Now if your app gets killed or swiped out then you can store the activity in SharedPreferences when activity is resumed or paused and on next start check in your spash which is Activity 'A' in your case, which was the last opened activity as you had stored the Activity name in SharedPreferences.

Example :

If you have three activities A-->B-->C

store the name in onPause() of all activities use SharedPreference to store its name :

@Override
protected void onPause() {
    super.onPause();
    SharedPreferences prefs = getSharedPreferences("Pref", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("lastOpenedActivity", getClass().getName());
    editor.apply();
}

And in your Splash which is here Activity A use this preference to checfk in your onCreate() -

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Class lastActivity;

    try {
        SharedPreferences prefs = getSharedPreferences("Pref", MODE_PRIVATE);
        lastActivity = Class.forName(prefs.getString("lastOpenedActivity", ParentActivity.class.getName()));
    } catch (ClassNotFoundException ex) {
        lastActivity = ParentActivity.class;
    }

    startActivity(new Intent(this, lastActivity));
    this.finish();
}
shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47