【问题标题】:How to avoid execute some part of codes within onCreate()如何避免在 onCreate() 中执行部分代码
【发布时间】:2012-08-13 00:27:06
【问题描述】:

我在处理 Activity 中的 onCreate() 时遇到了一个主要问题。就像thread 说的,我只能在主Activity 的onCreate() 方法中执行部分代码一次。所以我按照该线程中的步骤并执行以下操作:

/*I've updated the code to SharePreferences version*/

public class MyApp extends Activity {
   private static RMEFaceAppActivity instance;
   private boolean wascalled = false;

   private SharedPreferences sharedPreferences;       
   private SharedPreferences.Editor editor; 


   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     setContentView(R.layout.main);

      //initiate some buttons here

     sharedPreferences = this.getSharedPreferences("test",0);  
     editor = sharedPreferences.edit();  

    if(sharedPreferences.getString("wascalled", "false").equalsIgnoreCase("false"))
    {
        //work can only be done once
        editor.putString("wascalled", "true");
        editor.commit();
    }
        //set buttons to listener methods

   }

   void onClick(View arg0)
   {
      if(arg0.getId() == R.id.button1)
      {
         Intent intent = new Intent(this, MyChildApp.class);
         startActivity(intent);
      }
   }

}

在 MyChildApp 类中,我在那里完成工作时调用了finish()。但是,wascalled 字段始终为 false。我认为当从MyChildApp 返回时第二次执行onCreate() 时,wascalled 应该已经设置为true。然而事实并非如此。并且每次从MyChildApp返回时,都会执行onCreate()方法中的if语句中的代码。

有人对此有什么建议吗?提前非常感谢。

【问题讨论】:

    标签: android oncreate


    【解决方案1】:

    定义 SharedPreferences 并最初存储一个值 0/false 以表明 wascall() 从未被调用。

    现在,当第一次调用 wasCalled 时,将此 SharedPreference 变量的值更新为 1/true

    下次运行onCreate()时,检查SharedPreference中变量的值,如果值为1/true,则不要再次执行。

    实现 SharedPreferences 的代码:

    final String PREF_SETTINGS_FILE_NAME = "PrefSettingsFile";
    int wasCalledValue;
    
    onCreate() {
    
    ....
    
    
    SharedPreferences preferences = getSharedPreferences(PREF_SETTINGS_FILE_NAME, MODE_PRIVATE);
    wasCalledValue=  preferences.getInt("value", 0); 
    
    if(wasCalledValue == 0)
    {
    // execute the code
    //now update the variable in the SharedPreferences
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("value", 1);
    editor.commit();
    
    }
    
    else if(wasCalledValue == 1)
    {
    //skip the code
    }
    
    } // end of onCreate()
    

    【讨论】:

    • 非常感谢您的回答!我在问题中更新时定义了一个 sharedPreferences 。但是代码仍然被执行。我真的不知道为什么。你能帮我解决这个问题吗?谢谢!
    • 非常感谢!这有效:) 这次不执行 onCreate() 中的 if 语句中的代码。非常感谢您的帮助。
    【解决方案2】:

    当您从 MyChildApp 返回时,myApp 正在重新创建,因此再次调用 onCreate,并再次初始化变量(这就是 wascall 始终为 false 的原因)。

    一种可能的解决方案是将被调用状态存储在 SharePreferences 中

    【讨论】:

      【解决方案3】:

      wascalled 将始终为 false,因为它是 Activity 实例的一部分。它已在您的活动中声明:

      private boolean wascalled = false;
      

      重新创建活动时,所有实例变量都被初始化为其默认值,这就是为什么您总是得到false

      如果您注意来自 thread 的代码,您会注意到 wascalled 是另一个类的一部分,而不是 Activity 的类。

      if (!YourApplicationInstance.wasCalled) {
      }
      

      在这个具体示例中,YourApplicationInstance 是一个单独的类,它保持wascalled 变量的状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 2020-03-17
        • 2020-07-04
        相关资源
        最近更新 更多