views:

77

answers:

1

Hi Folks,

Basically, we are maintaining the sessions for the specific user's environment. For that, Three basic stuff we have to maintain. That are Login, Session check and Logout.

In Android, Each screen designed by Activity. we can startActivity() and finish() a current Activity. But you can not do that Previous Activity.

UI Design of APP:

Login(Main Activity)-->Home(Child Activity)-->It contains Profile, add data, settings,etc.(Sub Child Activity). All Screen has a Logout option in Menu.

The Problem is:

When i select a Logout. The App gets Logout. But I can not navigate the App to the Login Screen. The Previous child Activity did not finished from the Activity Stack.

Any Idea to Achieve this? Its Most Thankful.

A: 

A template pattern could nicely fit in here. (pseudocode following)

public class BaseActivity extends Activity{

   protected void onCreate(savedInstance){
      if(user is logged in){
         onLoggedIn(savedInstance);
      else{
         //TODO: clear some flags in your object model which identify
         //the case when the user is logged in

         startActivity(this,LoginActivity.class);
         finish();
      }
   }

   protected abstract void onLoggedIn(savedInstance);
}

public class SomeActivity extends BaseActivity{

   protected void onLoggedIn(savedInstance){
      //normally do activity stuff
   }
}

Warning: I didn't try this out. It's just an idea which came me to mind right now.

Basically you have your base Activity which has the onCreate(...) method where you check whether the user is logged in. In the case it's logged in you call an abstract method "onLoggedIn(...)" otherwise you start the login activity and clear any login information in your object model.

Juri