omega
omega

Reputation: 43973

Android activity getting old values in bundle

In my android activity B, I read values bundled in the intent like this

    Bundle bundle = getIntent().getExtras();
    Boolean mine = bundle.getString("mine").equals("1");
    int pagenum = bundle.getInt("page");

When I start B from another activity A, I give in mine=0,pagenum=0. And I can read that fine in B.

But then in B, I want to reload the activity, by finishing itself and opening another B. I also need to pass in the new data like this:

private void refresh(Boolean mine, int newpage) {
    finish();

    Intent myIntent = new Intent(this, AllThreadsScreen.class);
    myIntent.putExtra("mine", mine ? "1" : "0");
    myIntent.putExtra("page", Integer.toString(newpage, 10));
    startActivity(myIntent);
}

When I call this, I make sure that newpage has a value of 1. However the problem is that, after starting the activity, when I read the page value from the bundle, it becomes 0 again...

Does anyone know whats wrong?

Thanks.

Upvotes: 0

Views: 361

Answers (1)

Jim
Jim

Reputation: 10288

You are not "restarting" the Activity. If you call finish() and the lines after still work, then it ins't really finished, is it?

What is happening is the Activity may begin the process of finishing, but then receives a new Intent. Android doesn't actually destroy it.

Also, you don't need to finish the Activity anyway. just use onNewIntent (a little known, little used Activity method).

How to capture the new Intent in onNewIntent()?

Upvotes: 1

Related Questions