Valentin Baryshev
Valentin Baryshev

Reputation: 2205

How can I make one Bundle for all activities?

I use NavigationDrawer with activities. And all activities are extended by a root activity which implement NavigationDrawer and it's base functions. I have many different activities in which I want to saveInstanceState, I want to store data in Bundle.

Here is example: I have activties A,B I do some changes in A, than I startActivity(B)! I guess A saves it's state in some Bundle before starting B. I want to pass A's Bundle to B activity (how can i do that? that's the main question)

Now i am in B activity! Finally i want to startActivity(A) with restored istance state.

How can i do that?

Upvotes: 1

Views: 142

Answers (2)

Nick H
Nick H

Reputation: 8992

You could create a new Bundle object and put in all of the data you want to send to Activity B. Then when you create your intent, pass the bundle into it like this;

Bundle bundle = new Bundle();
bundle.putString("string_extra","Extra Data, String!")
Intent intent = new Intent(this,ActivityB.class);
intent.putExtra("bundle_extra",bundle);
startActivity(intent);

Then inside ActivityB, you can call this,

Bundle extraData = getIntent().getBundleExtra("bundle_extra");

Once you have your bundle, you can just grab your variables out of it inside ActivityB.

Upvotes: 1

Roberto Betancourt
Roberto Betancourt

Reputation: 2515

You can do something like this if you want to send data between activities:

    Intent intent=new Intent(B.class);
    Bundle bundle=new Bundle();
    bundle.putString("myKey","MyValue");
    intent.putExtras(bundle);
    startActivity(intent);

And to retrieve that information from Activity B:

        Bundle bundle=getIntent().getExtras();

Upvotes: 1

Related Questions