Issue
I have an android app, which has HomeActivity
as the parent activity of all other activities. meaning - I always have an instance of it, and if you press the back button from any activity it will take you to this activity.
Now, there is an option to launch the app from a notification.
When the user clicks the notification, I start RidesActivity
(one of the other activites), and in this case if I press back, it closes the app (no instance of HomeActivity
).
What can I do in order to start an instance of HomeActivity
to the background, and make sure that if the user click back, it will go to HomeActivity
?
Solution
Android goes back to the last activity when you press the back button, but at your point there is no last activity, because you directly start the RidesActivity.
To resolve this you can start your MainAcivity via the Intent from the notification instead of your RidesActivity and put some extra information for starting directly the RidesActivity.
Code for your notification:
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //is important to start the activity from a notification
intent.putExtra("startRidesActivity", true);
startActivity(intent);
Code for your MainActivity, onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Put the following at the end of this method
*/
Bundle bundle = getIntent().getExtras();
if(bundle!=null && bundle.containsKey("startRidesActivity")){
Intent intent = new Intent(this, RidesActivity.class);
startActivity(intent);
}
}
That's it. Now your Notification opens the MainActivity and the MainActivity start directly your RidesActivity. If you click then the back button, you should see your MainActivity and not your homescreen.
Answered By - Larcado
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.