I want to create a button that would lead the user straight back to main activity which doesn't have the android name="com.example.example".
It has android.intent.etc...
How can I reference my button to go back to this activity?
Asked
Active
Viewed 4.0k times
24
-
thanks for editing, (have bad english) – Moussa Jul 12 '12 at 21:26
-
what is the full name of the Activity you are trying to start ? – confucius Jul 12 '12 at 21:28
-
Main Activity, hers is the
-
@Moussa The accepted answer is wrong since it will add a new instance of Home activity to the stack, it's like create your activity again and again!! – K_Anas Jul 12 '12 at 21:54
-
Ok zzzzzzzzzzzzz fixed the answer, thanks for your help – Moussa Jul 13 '12 at 17:47
6 Answers
45
Lets say your main activity is called Main.java.
btnBack.setOnClickListener(new OnClickListener(){
private void onClick(){
Intent intent = new Intent(currentActivity.this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});

Peter Lozovitskiy
- 239
- 3
- 10

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
- 5,711
- 8
- 54
- 70
-
This will add a new instance of Home activity to the stack, not take the user back to the Home activity. – E-Riz Jul 12 '12 at 21:48
-
-
2I added the flag so it doesn't add a new instance to the stack. – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Jul 13 '12 at 17:13
15
use startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

K_Anas
- 31,226
- 9
- 68
- 81
-
1+1: This is the correct response. Adding flags like NEW_TASK will only create unexpected behavior for the user, and simply starting a new activity will add a second (new) instance to the stack. – devunwired Jul 12 '12 at 21:34
-
1@Moussa Intent a = new Intent(this,Home.class); a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(a); – K_Anas Jul 12 '12 at 21:50
10
Sometimes you can just call activity.finish()
to end current activity, so the main (first created) activity will pop out.
If this is not your case, do this:
Intent intent = new Intent(getApplicationContext(), Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);

Adam Stelmaszczyk
- 19,665
- 4
- 70
- 110
5
Intent intent = new Intent(this, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

confucius
- 13,127
- 10
- 47
- 66
-
This is my setup: public void onClick(View v) { // Perform action on click Intent intent = new Intent(this, Main.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); This is the error: The constructor Intent(new View.OnClickListener(){}, Class
) is undefined'. – Moussa Jul 12 '12 at 21:41 -
-
0
Well from wherever you are just call startActivity() with the required parameters inside the buttons onClick method. That's it.

Code Droid
- 10,344
- 17
- 72
- 112
0
public void onBackPressed(){
finish();
}

Jupiter
- 615
- 1
- 6
- 15
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/16343576) – Zoe Jun 07 '17 at 11:28
-
@LunarWatcher I respectfully disagree - this _is_ an _attempt_ to answer... An attempt that is perhaps faulty and happens to work in very specific cases - but a solution being bad is not reason enough for deletion. – Dev-iL Jun 07 '17 at 14:53
-
It is 5 years too late and wrong, but the key here is that it requires clarification if someone is new to ANdroid for an instance. What is this, where is it placed, etc – Zoe Jun 07 '17 at 15:18
-