2

I have a major problem dealing with onCreate() in Activity. Like the thread said, I can only execute some part of codes in onCreate() method of the main Activity once. So I followed the steps in that thread and do the following:

/*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);
      }
   }

}

And in the MyChildApp class, I called finish() when work is done there. However, the field wascalled is always false. I think when the onCreate() is execute the second time when returning from MyChildApp, wascalled should be set to true already. However it isn't. And the code within the if statement in onCreate() method is executed every time when come back from MyChildApp.

Does anyone have advices on this? Thanks a lot in advance.

Community
  • 1
  • 1
Iam619
  • 795
  • 2
  • 12
  • 28

3 Answers3

1

Define SharedPreferences and store initially a value 0/false to show that wascalled() was never called.

Now, when wasCalled is called for the first time, update the value of this SharedPreference variable to 1/true.

Next time when your onCreate() runs, check for the value of the variable in the SharedPreference and do not excute it again if the value is 1/true.

Code to implement 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()
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • Thank you very much for the answer! I defined a sharedPreferences as I updated in my question. However the codes are still executed. I really cannot figure out why. Can you help me on this? Thanks! – Iam619 Aug 16 '12 at 16:24
  • Thank you very much! This works:) The code within if statement in onCreate() is not executed this time. Really appreciate your help. – Iam619 Aug 16 '12 at 16:51
0

When you're returning from MyChildApp, myApp is recreating, so onCreate is called again, and variables are initialized again(this is the reason why wascalled is always false).

One of the possible solutions is to store wascalled state in SharePreferences

Mike
  • 819
  • 1
  • 8
  • 14
0

wascalled will always be false because it is part of the Activity instance. It was declared in your actrivity:

private boolean wascalled = false;

When the activity is recreated all the instance variables are initialized to their default value, that is why you get always false.

If you pay attention to the code from that thread, you'll notice that the wascalled is part of another class, and not Activity's class.

if (!YourApplicationInstance.wasCalled) {
}

In this concrete example, the YourApplicationInstance is a separate class that keeps the state of wascalled variable.

Community
  • 1
  • 1
Andy Res
  • 15,963
  • 5
  • 60
  • 96