Skip to content Skip to sidebar Skip to footer

Button To Go Back To Mainactivity

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

Solution 1:

Lets say your main activity is called Main.java.

btnBack.setOnClickListener(new OnClickListener(){

  privatevoidonClick(){
    Intent intent = new Intent(currentActivity.this, Main.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
    startActivity(intent);
  }
});

Solution 2:

use startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

Solution 3:

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 = newIntent(getApplicationContext(), Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)             
startActivity(intent);

Solution 4:

Intent intent = newIntent(this, Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

Solution 5:

Well from wherever you are just call startActivity() with the required parameters inside the buttons onClick method. That's it.

Post a Comment for "Button To Go Back To Mainactivity"