1

My App starts and loads the first screen, then the users changes to the next screen by clicking a button with:

    Intent intent = new Intent(DashboardActivity.this, LoginActivity.class);
            startActivity(intent);

and then from there, the user changes to the next screen, again with:

    Intent intent = new Intent(LoginActivity.this, NextActivity.class);
            startActivity(intent);

now there should be 3 screens on the stack and in this last screen, I want to go all the way back to the first screen, how do I do that with one click? I want to send putExtra("") data from that last screen to the first screen.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
user934779
  • 303
  • 1
  • 8
  • 14

3 Answers3

2

You can do this by

Intent intent = new Intent(NextActivity.this, DashboardActivity.class);
// This will remove all activities which are on the top of DashboardActivity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.putExtra("<Your data will be here>");
startActivity(intent);

Hope this will help you

silwar
  • 6,470
  • 3
  • 46
  • 66
2

Add this

In NextActivity,

Intent myIntent = new Intent(NextActivity.this, DashBoardActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  myIntent.putExtra("UserName",UserName);
   startActivity(myIntent);

In DashBoardActivity,

Intent intent = getIntent();
UserName=intent.getStringExtra("UserName");
KMI
  • 496
  • 4
  • 24
1
 Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 intent.putExtra("string",strValue);
 startActivity(intent);
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134