简体   繁体   English

以前打开活动时如何避免 OnCreate 方法执行 (Xamarin.Android)?

[英]How to avoid OnCreate method execution when activity has been opened previously (Xamarin.Android)?

I have two activities FirstActivity and SecondActivity .我有两个活动FirstActivitySecondActivity When I am in FirstActivty I call the SecondActivity like that:当我在FirstActivty时,我像这样调用SecondActivity

var secondActivity = new Intent(this, typeof(SecondActivity));
secondActivity.PutExtra("someExtra", someExtra);
StartActivity(secondActivity);
Finish();

In SecondActivity I call the FirstActivity in OnBackPressed method:SecondActivity我所说的FirstActivityOnBackPressed方法:

public override void OnBackPressed()
{
    StartActivity(typeof(FirstActivty));
    Finish();
}

I looked at answers regarding that question for Android(Java) .我查看了有关该问题的答案Android(Java) The answers were that in order to avoid OnCreate method to be executed in FirstActivity when the activity has been already created, I don't have to destroy FirstActivity after I open SecondActivity , I have to remove Finish() after StartActivity(secondActivity);答案是,为了避免OnCreate中要执行的方法FirstActivity活动已经被创建的时候,我没有破坏FirstActivity后,我打开SecondActivity ,我不得不删除Finish()StartActivity(secondActivity); line.线。 I removed it but OnCreate method still gets executed when I go back from SecondActivity .我删除了它,但是当我从SecondActivity返回时, OnCreate方法仍然被执行。 Does this solution work only in Android(Java) ,and if yes, what is the solution for Xamarin.Android ?此解决方案是否仅适用于Android(Java) ,如果是, Xamarin.Android的解决方案是什么?

You can add LaunchMode = Android.Content.PM.LaunchMode.SingleInstance in your FirstActivity attribute like this code.您可以像此代码一样在FirstActivity属性中添加LaunchMode = Android.Content.PM.LaunchMode.SingleInstance

   [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true,LaunchMode = Android.Content.PM.LaunchMode.SingleInstance)]
    public class MainActivity : AppCompatActivity
    {
        
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
           
        }

Here is running GIF.这里正在运行 GIF。

在此处输入图片说明

If you do not want to execute the code in the Oncreate method, You can refer to this thread.如果不想执行Oncreate方法中的代码,可以参考这个线程。 https://stackoverflow.com/a/6931246/10627299 https://stackoverflow.com/a/6931246/10627299

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

相关问题 如何将 Activity 类型传递给 Xamarin.Android 中的方法? - How to pass Activity type to a method in Xamarin.Android? Android - 如何在activity的onCreate()中调用setTitle()时避免延迟 - Android - How to avoid delay when calling setTitle() in activity's onCreate() 当我按下“后退”按钮时,如何避免调用当前活动的onCreate方法 - How can I avoid calling onCreate method of current activity when I press Back button 更新尚未打开的活动 - Updating an activity that has not been opened 如何在启动Activity时避免调用onCreate()? - How do I avoid onCreate() being called when starting an Activity? 保存我的活动 xamarin.android 的状态 - Save the state of my Activity xamarin.android Android Activity onCreate方法调用的同步方法 - Synchronized method called by Android Activity onCreate method Xamarin.Android的便携式作物图像活动(Android的Monoodroid / Mono) - Portable Crop Image Activity for Xamarin.Android (Monodroid / Mono for Android) 在活动开始而不是onCreate上运行的Android方法 - Android Method to run on activity start and not onCreate 如何重新启动活动并在Android SDK &lt;11中保存所有先前打开的活动 - How to restart Activity and save all previously opened activities in Android SDK < 11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM