Android ending activities - java

I go through few activities and when I want to go back to main window I use FLAG_ACTIVITY_CLEAR_TOP but if I press back button then I would go back to other activities that I went through.
Briefly speaking I want to go to main activity and after presing back button it wouldn't go to other activities.
EDITED
This clossing all activities is used to complete logoff process in another words it shouldn't user let to go back. I can't use BackPressed(), because I am using putExtra and getExtra. Maybe someone could write small sample. Because I don't understand flag system.

Use FLAG_ACTIVITY_NO_HISTORY on Activities you don't want saved on the stack. When the user presses back he/she will go to the last activity that didn't have that flag set.

You can override onBackPressed in your activity and start your desired one with the flags:
FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP

Related

How to navigate back to my app instead of going down the backstack?

Let's say the user presses a button which causes another activity to get launched. Now the user is in another activity. What flag do I have to add to the intent, so that the user returns to my app when pressing the back button instead of navigating down the back stack of the started activity?
From the documentation, "activities on the backstack are never rearranged."
Android documentation Tasks and BackStack
You do not have to add anything to the intent. When you navigate out of you app and start another activity to complete a task, the new activity becomes kind of like an extension of your app. Pressing the back button takes you back to your app. Read This

Exit from Application android

I tried some way to exit from application but each method just minimizes the app. I'd like to close down the app not minimize it. Exit Button is in MainAcitivity.
For now i'm calling this function, it works but not as i need.
public void AppExit()
{
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
I know this question is asked a lot but i can't find solution exactly what i need.
After your advice i use this function
public void AppExit()
{
System.exit(0);
finish();
}
If you want to exit why you are starting activity again.
use only this.finish(), remove other statements.
Take look at activity life cycle for more info
Few things. In your code you can put finish(); after startActivity, this may help (did for me).
How it should be done however is the way the guidelines have it already. The back button does close the app. If you press the back button and then restart your app, you will be on the very first page activity, not where you left off.
In regards to the back button, you can Override it to control what it does if you are not happy with it. You can put your code in there and when the users presses the back button it will execute the code. Users are more accustomed to pressing the back button rather than a button within a layout (which I presume is what you may be doing).
This might be helpful if you are sticking to what you have: How to clear the Android Stack of activities?
Try with this single line code :
System.exit(0);
You need to navigate to your main activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, pass it some data that it will know that you are exiting the application. And just call finish from your main activity.
Another suggestion is Kamlesh Arya answer. How ever it is a bad practice as it will not destroy the activity stack, and next time the use lunch your application it will not start from the main activity but from the last active activity.

How would i resume the last viewed activity after the user has been closed the application?

How would i resume the last viewed activity after the user has been closed the application. It is like, If the user open the application, the first activity will appear and when the user click the button it will proceed to the next activity, if he close the application and open again it will still show the last viewed activity. how would i do that? please help me.
Thank you,
I think you should use sharedpreferences. And in every activity you open, you have to put a value on your sharedpreference so that if you open again your application it will check the value of your shared preference then compare it, call the activity that fits the value you got. Hope it helps :)
You need to track his last activity then, keep it in a database and whenever he returns, look up his last activty in the database...the other way is cookies but not adviseable ....
Place this below line in AndroidManifest.xml for your activities in <activity/>.
android:launchMode="singleTask"

Android application design help, How to control which activity shows

Hello I require some advice on how to approach this problem,
bear with me as I am still new to android.
I have an activity that opens on application start and requires some information from the user, but next time the user opens the app, i want the application to open a different activity that will display the various information.
Kind of like facebooks app, where when you first run it, you have to login, and only then next time you run the app you are guided straight to the feed.
Any ideas how one could do this efficiently?
UPDATE: Ive stored the information via shared preferences and am now using a controller activity that decides which step to take.
So, Controller activity runs on start up, and decides whether to show a log in screen or whether to go straight to the information. But now im encountering a problem where i end up opening a blank activity (the controller) and then another ontop of that ( the decided activtiy). I dont want the blank activity to show, so its kinda of like a background process, any ideas?
Ideally you would have a main activity like a controller. Use a SharedPreference object to keep track of whether the user is logged in or not. So back in your main activity, read this value and if it is set go to your news feed activity else show a login screen activity. (as you do the check and redirection, you can show a progress dialog)
links for SharedPreferences
MobTuts
Android Developer

For programming an android app: How do I make it display a one time setup screen

While searching this question I have seen this. One time Android setup screen?
It asks my question, but I can never get it to work. What I want to happen is when I first startup the app, it gives me the setup screen. Then when I press save, I want it to quit the app. When I click the app again, I want it to preform a task, rather than show up with a screen. So thats really 2 questions, how to make it show a setup screen one time, and then how to make it do an action(by clicking on the app) without a screen showing up at all.
Right now, I use SharedPreferences editor to input my settings.
Pretty simple, in the onCreate of your activity:
savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if(savedPreferences.getBoolean(PREF_SHOW_ABOUT_ON_APP_START, true)){
Intent intent = new Intent(this, SetupActivity.class);
startActivity(intent);
savedPreferences.edit().putBoolean(PREF_SHOW_ABOUT_ON_APP_START, false).commit(); // YOu could do this line within the SetupActivity to ensure they have actually done what you wanted
finish();
} else {
// Go somewere else
}
You don't have to activity switch, but you get the picture
For the first time only set up screen, you simply have to use SharedPreferences. Check if a value, alreadyLaunched exists in the shared preferences. If not, show the set up screen. If yes show your app.
For the second part of your question, it would be considered very bad practice to have an icon app that does nothing at all when you click it. How would the user know that it worked or did what it was supposed to do ?
For the "I want a task to execute but I don't want the user to see it happening", I believe you should look at services. When your user closes the app clicking the save button on the set up screen, start a service ( http://developer.android.com/reference/android/app/Service.html )
When the user clicks again on your app, simply show a message and make the app call the service via an intent.
Probably you are forgetting to call
myEditor.commit()
after you are done with setting your preferences.

Categories