问题描述
晚上,
我现在面临一个小问题,我不能得到固定。我有一个'基地'活动:在这历史的数组列表(名称测试活动
,延长的ActivityGroup
) 。当我去一个新的活动,我用下面的位加载一个新视图每次。
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);
正如你可以看到我打电话 replaceView
方法替换的观点。见下面的code:
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);
}
在每一个活动,我重写 onBack pressed
:
In every activity I'm overriding the onBackPressed
:
@Override
public void onBackPressed()
{
TestActivity.group.back();
}
见下面的回
方法:
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();
}
}
现在问题来了:当我用我的手机和previous活动上的后退按钮来到前台什么都不会在视图中(活动)被调用。当我按照活动的生活cyle我认为 onResume
将被调用,但事实并非如此。什么也没有发生。
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.
所以,问题是:我怎么能叫 onResume
方法,当我pressing后退按钮
So the question is: How can I call the onResume
method when I'm pressing the back button?
推荐答案
@Commonsware好心指出,(在我的另一篇文章)的活动组实际上已经去precated。我们应该被移动到对片段,而不是
@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.
一直由同一个问题,因为你争夺的时间比我愿意承认。当你以后pressing后退按钮从的ActivityGroup内返回焦点视图的常用工具(OnResume,OnRestart)无工作。
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.
不幸的是,我还需要找到一种方法来的假的时候,从后退按钮后的ActivityGroup内为pressed(因为我投入巨资在这个传统$ C $的onResume C,它使用活动组。)在我的斗争。
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.
希望这有助于。
- 编辑 -
我发现了一个办法做到这一点:
I found a way to do this:
而不是保持意见的历史,保持启动活动的ID的历史。你开始每个活动都有一个与之关联的唯一ID。所以,当你回电话(),检索该活动的ID,并重新开始活动。这将触发您的活动的onResume()方法,你的愿望。
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.
下面是我会怎样重组code您提供:
以下位加载新的观点:
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");
在内部更换视图replaceView()方法:
Replacing the view within the "replaceView()" method:
public void replaceView(View v, String activityId)
{
history.add(activityId);
setContentView(v);
}
最后,从内部的ActivityGroup回()方法:
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 :)
这篇关于呼叫onrestart / onresume当点击后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!