switching between activities in android - android

Hi I'm writing an app that has multiple activities. Right now it starts at the home screen, then when the user presses a button it starts a new activity and goes to another screen, then the user enters in information and presses a button to start another activity and another screen.
I have a menu setup so that from whatever the activity the user is in they can get back to the home screen. What I want it to do is kill all the current activities and just take the user right back to the home screen, so there is only one activity running again. How can I do this?

After sending the Intent just call finish() and the activity you're leaving will be closed.
Just don't do that on your "homescreen" activity. That way whenever the user starts an activity through one of your activities when he presses the back hardware button he is going to get back to your "homescreen" activity.

I figured it out. If you add myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); then when you run startActivity(myIntent); it clears all activities except the one myIntent is starting.

Related

Find out where back button press is going

When user presses a back hardware button in my activity he will either be taken to a previous activity or to the home screen (depends on how he got into that activity in a first place). Is there a way to determine where exactly user will be taken inside of onBackPressed method?
In your home and previous activity when you start the intent to go to this activity, you can add an extra to the Intent.
Intent.setExtra("PREVIOUS_ACTIVITY ", "HOME")
Then you can check in your activity onBackPressed() if it came from the home or another activity.

how to open Home Activity from any another Activity without using Intent

In a interview someone asked me this question. Suppose there are 4 activities in application. He wants back button to work normally on first three activities(i.e. going to previous screen on back button press); except the last.
He wanted that, when he press back button on fourth activity(screen), user should go to Home Activity(Starting Screen or first screen). But the contents on Home Activity should be same when user went from first screen to second screen. So I think, I can't use Intent, as it will create new instance of Starting Activity.
He wanted that, when he press back button on last activity(screen), user should go to Home Activity(Starting Screen or first screen).
The interviewer should be interviewing for his or her own replacement, if (s)he thinks that hacking the BACK button this way is a good idea.
So I think, I can't use Intent, as it will create new instance of Starting Activity.
Add FLAG_ACTIVITY_REORDER_TO_FRONT or the combination of FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP to the Intent used with startActivity() to bring up your "home activity". Either will cause the existing "home activity" instance to come to the foreground. If you want all other activities to be destroyed, use the second approach (FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP).

Android reuse activity from activity stack and clear it

I have application with main activity and some more.
On each other activity there is the logo of the application.
When user presses the logo button, I want to get back to the main activity.
I do not want to create new intent, since activity aready on the activity stack.
How can I get it - use the one on the stack?
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Yoav
I do not want to create new intent, since activity aready on the activity stack.
If you start an activity (via intents or any other way) which was already started and is on the stack , then Android just takes that same instance of the activity and places it on top of the stack. A new instance is not created. Ofcourse this happens if you did not manually kill the activity (by calling finish() in it).
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Its not recommended to override the back button to quit the application in every activity(Unless your app has strong reasons to do so). generally the app should let the user go back to the previous activity when he presses the back button (which is what a user might be expecting).
If you still would like to quit with the back button then you can override the back button function and launch the intent that leads to the home screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I faced a somewhat similar problem. The following link might be helpful for you:
clearing stack of activities with just one press
Its very simple.
Don't call finish() on Home/Main activity.
For Ex: Say you have 4 activities.. If your requirement is like this .. Act1-->Act2-->Act3-->Act4-->Act1 . So, don't call finish() on Act1. But call finish() on Act2, Act3 while you are going to other activity. So when you click on logo in Act4, just call finish(). So, automatically you will come back to Act1 which is your Main activity.
If you have logo in Act2, Act3 also then call finish() on click of logo to go back to Main. But remember to call finish() on Act2 while you are going from Act2 to Act3

block back button for ceratin activities

I have two activities A and B. What I want is to show activity A as an intro, the user will be unable to navigate back to it. Is there some flag I can set to activity A to do this? Can I block the back button for one activity only? Activity A is of course my main activity which automatically starts activity B after some "hard work".
Best regards.
you do not need to block the back button, but just call finish() on your A activity after firing an intent to start B. Back button pops the previous activity from activity stack and it won't be able to pop A if it is already finished.
For this you don't need to block the Back button. Simply, start the second Activity and quit the first one. And now if user presses the Back, they will be taken to the Android home screen not on your apps home screen.
Updates: By the way if you want to intercept the Back button for any reason, simply override the onBackPressed() method of Activity class. See this for details.
Never override the functionality of a hardware button.
You should call finish() in Activity A right after starting Activity B (calling the Intent).
it works but the application terminates and i'm redirected to android's applications screen. I would like to stay in activity B if back button is pressed, i don't want to exit the app. here's is what i got :
public void startProgram(Context context){
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish();
}

Android: How to clear activity stack from widget

Is there a way to clear activity stack from widget?
I have following situation:
From widget user presses button what starts new activity A. Next user move from A to new activity B. After that he presses home button which takes him back to home screen. Now if he presses button again, he will be moved directly to the activity B.
I thought ACTIVITY_FLAG_CLEAR_TOP would help, but it didn't.
Does activity A start activity B? If so you can start it with the FLAG_ACTIVITY_NO_HISTORY flag to specify not to put B in the history stack.

Categories

Resources