1

Possible Duplicate:
How do I pass data between activities in Android?

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.

Community
  • 1
  • 1
user934779
  • 303
  • 1
  • 8
  • 14

5 Answers5

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,304
  • 3
  • 47
  • 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
  • 498
  • 4
  • 24
1

its very simple , again you have to make one Intent which directs to the first activity and get your data there:

Dhruvil Patel
  • 2,872
  • 2
  • 21
  • 39
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,219
  • 16
  • 116
  • 133
1
Intent intent = new Intent(NextActivity.this, LoginActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   startActivity(intent );

this will clear all the Activities on the Top of current activity from activity Stack.

Sachin Gurnani
  • 4,421
  • 9
  • 41
  • 48