views:

10669

answers:

5

Hi,

I am creating an application which requires login. I created the main and the login activity.

In the main activity onCreate method I added the following condition:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ...

    loadSettings();
    if(strSessionString == null)
    {
        login();
    }
    ...
}

The onActivityResult method which is executed when the login form terminates looks like this:

@Override
public void onActivityResult(int requestCode,
           int resultCode,
           Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode)
    {
     case(SHOW_SUBACTICITY_LOGIN):
     {
      if(resultCode == Activity.RESULT_OK)
      {

       strSessionString = data.getStringExtra(Login.SESSIONSTRING);
       connectionAvailable = true;
       strUsername = data.getStringExtra(Login.USERNAME);
      }
     }
    }

The problem is the login form sometimes appears twice (the login() method is called twice) and also when the phone keyboard slides the login form appears again and I guess the problem is the variable strSessionString.

Does anyone know how to set the variable global in order to avoid login form appearing after the user already successfully authenticates?

Thanks!

A: 

You can have a static field to store this kind of state. Or put it to the resource Bundle and restore from there on onCreate(Bundle savedInstanceState). Just make sure you entirely understand Android app managed lifecycle (e.g. why login() gets called on keyboard orientation change).

alex
+45  A: 

The more general problem you are encountering is how to save state across several Activities and all parts of your application. A static variable (for instance, a singleton) is a common Java way of achieving this. I have found however, that a more elegant way in Android is to associate your state with the Application context.

As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application.

The way to do this is to create your own subclass of android.app.Application, and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):

class MyApp extends Application {

  private String myState;

  public String getState(){
    return myState;
  }
  public void setState(String s){
    myState = s;
  }
}

class Blah extends Activity {

  @Override
  public void onCreate(Bundle b){
    ...
    MyApp appState = ((MyApp)getApplicationContext());
    String state = appState.getState();
    ...
  }
}

This has essentially the same effect as using a static variable or singleton, but integrates quite well into the existing Android framework. Note that this will not work across processes (should your app be one of the rare ones that has multiple processes).

Soonil
Thank you Soonil -- these kinds of answers are the reason why I love Stack Overflow so much. GREAT JOB!
JohnnyLambada
A: 

This is exactly what I need, Awesome! However, how do I specify the class in the application tag in my manifest?

Raffe
+3  A: 

I couldn't find how to specify the application tag either, but after a lot of Googling, it became obvious from the manifest file docs: use android:name, in addition to the default icon and label in the application stanza.

android:name The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.

The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.

Mike Brown
+4  A: 

Create this subclass

public class MyApp extends Application {
  String foo;
}

In the AndroidManifest.xml add android:name

Example

<application android:name=".MyApp" 
       android:icon="@drawable/icon" 
       android:label="@string/app_name">
ebuprofen