Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start new Activity and finish current one in Android? [duplicate]

Currently I'm starting a new Activity and calling finish on a current one.

Is there any flag that can be passed to Intent that enables finishing current Activity without a need to call finish manually from code?

like image 607
pixel Avatar asked Jul 03 '12 09:07

pixel


People also ask

How do I finish my current activity and start a new one?

Use finish like this: Intent i = new Intent(Main_Menu. this, NextActivity. class); finish(); //Kill the activity from which you will go to next activity startActivity(i);

How do you finish current activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do I add a new activity to an existing project?

Create the second activity In the Project window, right-click the app folder and select New > Activity > Empty Activity. In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name.

What is the method start activity () in android?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.


Video Answer


3 Answers

You can use finish() method or you can use:

android:noHistory="true"

And then there is no need to call finish() anymore.

<activity android:name=".ClassName" android:noHistory="true" ... />
like image 62
Simon Dorociak Avatar answered Nov 11 '22 14:11

Simon Dorociak


Use finish like this:

Intent i = new Intent(Main_Menu.this, NextActivity.class);
finish();  //Kill the activity from which you will go to next activity 
startActivity(i);

FLAG_ACTIVITY_NO_HISTORY you can use in case for the activity you want to finish. For exampe you are going from A-->B--C. You want to finish activity B when you go from B-->C so when you go from A-->B you can use this flag. When you go to some other activity this activity will be automatically finished.

To learn more on using Intent.FLAG_ACTIVITY_NO_HISTORY read: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY

like image 28
Avi Dhiman Avatar answered Nov 11 '22 16:11

Avi Dhiman


FLAG_ACTIVITY_NO_HISTORY when starting the activity you wish to finish after the user goes to another one.

http://developer.android.com/reference/android/content/Intent.html#FLAG%5FACTIVITY%5FNO%5FHISTORY

like image 26
10s Avatar answered Nov 11 '22 14:11

10s