24

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Moussa
  • 851
  • 3
  • 10
  • 19

6 Answers6

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);
  }
});
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
  • what is the name of the class that contains the onClick method – confucius Jul 12 '12 at 21:46
  • Thanks for your help very much but zzzzzzzzzz answer helped me – Moussa Jul 12 '12 at 21:47
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
  • Its a nice hack – Goodlife Nov 27 '17 at 16:17