Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish Activity when starting other activity in Android?

I have problems to finish the activity before. I want to start another activity and finish the current activity. When I used finish it didn't exit the current activity.

How can I exit the activity before?

like image 658
mrhands Avatar asked Sep 23 '13 10:09

mrhands


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 I close an activity from another activity?

In Activity [A], on button click, I am calling Activity [B] without finishing Activity [A]. Now in Activity [B], there are two buttons, New and Modify. When the user clicks on modify then pop an activity [A] from the stack with all the options ticked..

How do I start an app from another activity?

To allow other apps to start your activity in this way, you need to add an <intent-filter> element in your manifest file for the corresponding <activity> element.


1 Answers

You need to intent your current context to another activity first with startActivity. After that you can finish your current activity from where you redirect.

 Intent intent = new Intent(this, FirstActivity.class);// New activity  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  startActivity(intent);  finish(); // Call once you redirect to another activity 

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - Clears the activity stack. If you don't want to clear the activity stack. PLease don't use that flag then.

like image 125
Vikalp Patel Avatar answered Oct 13 '22 07:10

Vikalp Patel