Kumar Bankesh
Kumar Bankesh

Reputation: 258

Resuming last activity on icon press to set result

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);

Upvotes: 2

Views: 975

Answers (2)

user320676
user320676

Reputation: 384

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

Upvotes: 3

shadygoneinsane
shadygoneinsane

Reputation: 2252

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();
}

Upvotes: 0

Related Questions