i am experiencing some issue with my application that I cant figure out how to solve. When I start my application, my dashboard activity is launched and some network task is perfomed (download information in background). Then, I hit the news button and I am taken to the news activity, where I again download information in background to populate my list view with the latest news. To launch the activity news I use the following code:
public void onNewsClick(View v) {
startActivity(new Intent(this, NewsActivity.class));
}
Inside the news activity I have an action bar with a home button, where the user should press to get back to the dashboard. If I hit the android back button, the dashboad opens ok and the data the was downloaded the first time the dashboard was launched is not downloaded again (expected scenario). However, if I hit the home button, the dashboard opens and the data is downloaded again. The home button dispach the following code when pressed:
public static Intent createIntent(Context context) {
Intent i = new Intent(context, DashboardActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return i;
}
I would expect the dashboard to not download the data again if the home button is pressed. Furthermore, If I start the news activity again, the list of latest news will be downloaded again, which is causing a lot of traffic and i dont want it.
So I have two questions:
1. How can I take the user back to the dashboard whithout loading the data again?
2. How can I go back a secont time to my news activity whithout loading the list again?
Many thanks for any answer!
T
1.) Try adding
to you intent. As it is, your dashboard activity is getting finished and then recreated. This will ensure your dashboard activity is brought to the top if it is already running and not recreate it.
2.) Adding this flag to your NewsActivity intent will allow you to not have to reload the NewsActivity if you back out to the dashboard through the action bar home button.
Edit
Adding this to your Dashboard Activity will ensure the application is exited when back is pressed.