简体   繁体   English

与Xamarin Droid中的TabGroupActivity一起使用时,未在子活动中调用OnResume()

[英]OnResume() Not called in the child activity when used with TabGroupActivity in Xamarin Droid

I am using Tabgroupactivity in Xamarin to develop a droid app. 我在Xamarin中使用Tabgroupactivity开发了droid应用。 I have a TabGroupActivity with 4 child activities. 我有一个带有4个子活动的TabGroupActivity。

The OnResume() is not being called in any of the child activities. 在任何子活动中均未调用OnResume()。

Can Anyone please help me with the right solution why is it not being called!! 任何人都可以请我以正确的解决方案来帮助我,为什么不这样称呼它! or Is there any fault in my code? 或我的代码有什么错误吗?

TabGroupActivity.cs 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 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 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);
    }
}
}

Use the OnStart Method to do that. 使用OnStart方法执行此操作。 We use the this method to load/refresh some ui-data when the view gets displayed. 当显示视图时,我们使用此方法加载/刷新一些ui数据。

OnResume : The activity goes in Pause state when another activity comes over it. OnResume :当另一个活动经过时,该活动进入暂停状态。 In this case when user pressed back button then onResume get's called. 在这种情况下,当用户按下后退按钮时,将调用onResume get。

More infos and good explanation to activity lifecycle: See this answer 有关活动生命周期的更多信息和很好的解释: 请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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