我在Xamarin中使用Tabgroupactivity开发了droid应用。
我有一个带有4个子活动的TabGroupActivity。
在任何子活动中均未调用OnResume()。
任何人都可以帮助我提供正确的解决方案,为什么不这样称呼它!或我的代码有什么错误吗?
TabGroupActivity.cs
namespace RB.Droid
{
[Activity (Label = "TabGroupActivity")]
public class TabGroupActivity : ActivityGroup
{
private List<string> _idList;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
if (_idList == null)
_idList = new List<string> ();
}
public void StartChildActivity (string id, Intent intent)
{
intent.AddFlags (ActivityFlags.ClearTop);
var manager = new LocalActivityManager (this, false);
var window = MainTabActivity.localActivityManager.StartActivity (id, intent);
if (window != null) {
_idList.Add (id);
SetContentView (window.DecorView);
}
}
public override void FinishActivityFromChild (Activity child, int requestCode)
{
var manager = new LocalActivityManager (this, false);
var index = _idList.Count - 1;
if (index < 1) {
Finish ();
return;
}
MainTabActivity.localActivityManager.DestroyActivity (_idList [index], true);
_idList.RemoveAt (index);
index--;
var lastId = _idList [index];
var lastIntent = MainTabActivity.localActivityManager.GetActivity (lastId).Intent;
var newWindow = MainTabActivity.localActivityManager.StartActivity (lastId, lastIntent);
SetContentView (newWindow.DecorView);
}
public override bool OnKeyDown (Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
return true;
}
return base.OnKeyDown (keyCode, e);
}
public override bool OnKeyUp (Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
OnBackPressed ();
return true;
}
return base.OnKeyUp (keyCode, e);
}
public override void OnBackPressed ()
{
var length = _idList.Count;
if (length > 1) {
var manager = new LocalActivityManager (this, false);
var current = MainTabActivity.localActivityManager.GetActivity (_idList [length - 1]);
FinishActivityFromChild (current, 0);
}
}
}
}
SettingsActivityGroup.cs
namespace RB.Droid
{
[Activity (Label = "SettingsActivityGroup")]
public class SettingsActivityGroup : TabGroupActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your application here
StartChildActivity ("Settings", new Intent (this, typeof(Settings)));
}
}
}
Settings.cs
namespace RB.Droid
{
[Activity (Label = "Settings")]
public class Settings : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your application here
TextView tv = new TextView (this);
tv.Text = "Settings";
SetContentView (tv);
}
protected override void OnResume ()
{
base.OnResume ();
Toast.MakeText (this, "ON RESUME NOT CALLED", ToastLength.Long);
}
}
}
最佳答案
使用OnStart
方法执行此操作。当显示视图时,我们使用此方法加载/刷新一些ui数据。OnResume
:当另一个活动经过该活动时,该活动将进入“暂停”状态。在这种情况下,当用户按下后退按钮时,将调用onResume
get。
有关活动生命周期的更多信息和很好的解释:See this answer