简体   繁体   中英

call onrestart/onresume when click on back button

Evening,

I'm facing a small problem which I can't get fixed. I'm having one 'base' activity (name: TestActivity , extending ActivityGroup ) with a history array list in it. Every time when I'm going to a new activity I'm using the following bit to load a new view.

Intent intent = new Intent(Start.this, CitySelect.class);
View view = TestActivity.group.getLocalActivityManager().startActivity("cityselect", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
TestActivity.group.replaceView(view); 

As you can see I'm calling the replaceView method to 'replace' the view. See below for the code:

public void replaceView(View v)
{
    history.add(v);
    setContentView(v);
}

In every activity I'm overriding the onBackPressed :

@Override
public void onBackPressed()
{
    TestActivity.group.back();
}

See below for the back method:

public void back()
{
    if (history.size() > 0)
    {
        history.remove(history.size() - 1);

        if (history.size() > 0)
        {
            View v = history.get(history.size() - 1);
            setContentView(v);
        }

        else
        {
            finish();
        }
    }

    else
    {
        finish();
    }
} 

Now comes the problem: when I'm using the back button on my phone and the previous activity comes to the foreground nothing will be called in the view (activity). When I follow the Activity life cyle I thought that the onResume will be called but that's not the case. Nothing happens.

So the question is: How can I call the onResume method when I'm pressing the back button?

@Commonsware kindly pointed out (in another post of mine) that ActivityGroups have actually been deprecated. We are supposed to be moving onto to Fragments, instead.

Been battling with the same problem as you for longer than I care to admit. None of the commonly used tools (OnResume, OnRestart) work when you return the focus to a view from within an ActivityGroup after pressing the back button.

Unfortunately, I still need to find a way to fake an onResume when the from within the ActivityGroup after the back button is pressed (since I am heavily invested in this legacy code that uses ActivityGroups.) On I struggle.

Hope this helps.

-- edit --

I found a way to do this:

Instead of keeping a history of the views, keep a history of the id's of the the activities you start. Every activity you start has a unique id associated with it. So, when you call back(), retrieve the activity's id, and start the activity anew. This will trigger your activity's "onResume()" method as you desire.

Here is how I would restructure the code you provided: following bit to load a new view:

Intent intent = new Intent(Start.this, CitySelect.class);
View view =     TestActivity.group.getLocalActivityManager().startActivity("cityselect", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
TestActivity.group.replaceView(view,"cityselect"); 

Replacing the view within the "replaceView()" method:

public void replaceView(View v, String activityId)
{
    history.add(activityId);
    setContentView(v);
}

And finally, the back() method from within the ActivityGroup:

public void back()
{
if (history.size() > 0)
{
    history.remove(history.size() - 1);

    if (history.size() > 0)
    {
        //View v = history.get(history.size() - 1);
        //setContentView(v);

       LocalActivityManager manager = getLocalActivityManager();
       String lastId = mIdList.get(history.size()-1);
       Intent lastIntent = manager.getActivity(lastId).getIntent();
       Window newWindow = manager.startActivity(lastId, lastIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
       setContentView(newWindow.getDecorView());

    }

    else
    {
        finish();
    }
}

else
{
    finish();
}

}

Hope THIS helps a little more :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM