Imri Persiado
Imri Persiado

Reputation: 1889

Moving between activities

I have 2 activities, 1 which is my main page of my application and another one for registeration. The body of my main page is something like that:

 public void onCreate(Bundle savedInstanceState)
 {
     registerOperation();

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     System.out.println("Already logged in");
 }
 public void registerOperation()
 {
     //checks if I did logged in already. if true start a new Intent of my Registeration page and stop the current Intent.
 }

In my test I havn't registered yet so the condition in the registerOperation is true and the new activity is coming up but after few seconds the app stop. I used the registeration page already but I called it by a diffreny way and everything is fine so I guess there is something wrong in the way I call it. Also the output line after I call the registerOperation is exceuted while the register activity is running.

EDIT If you want to see a specific code let me know and I will update the post.

pass activity code.

    public void registerOperation()
    {
        SharedPreferences pref = getSharedPreferences("MyKid",MODE_PRIVATE);
        String email = pref.getString("email", null);
        String password = pref.getString("password", null);
        if(email == null && password == null)
        {
            this.stopService(this.getIntent());
            Intent register = new Intent(getBaseContext(), Register.class);
            startActivity(register);
        }
    }

EDIT logcat:

12-31 11:27:38.610: E/AndroidRuntime(28903): java.lang.RuntimeException: Unable to stop activity {com.example.android.location/com.example.android.location.Track}: java.lang.NullPointerException

Upvotes: 1

Views: 121

Answers (4)

Mahmoud Jorban
Mahmoud Jorban

Reputation: 972

//this for the second question

public void onCreate(Bundle savedInstanceState)
     {
         if (registerOperation()){
             System.out.println("Already logged in");
             return;
         } 

         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);


     }
     public void registerOperation()
     {
         //checks if I did logged in already. if true start a new Intent of my Registeration page and stop the current Intent.
     }

// And

public boolean registerOperation()
    {
        SharedPreferences pref = getSharedPreferences("MyKid",MODE_PRIVATE);
        String email = pref.getString("email", null);
        String password = pref.getString("password", null);
        if(email == null && password == null)
        {
            this.stopService(this.getIntent());
            Intent register = new Intent(getBaseContext(), Register.class);
            startActivity(register);
            return true;
        }
        return false;
    }

Upvotes: 2

Imri Persiado
Imri Persiado

Reputation: 1889

The reason my application has stopped is because I overrided the onStop method and inside it I used some un-initialized objects. I changed the way that my application is built and I guess that was the reason I forgot those objects.

Thank you for comments and help.

Upvotes: 0

Vikalp Patel
Vikalp Patel

Reputation: 10877

Try something like these::

Please try to remove unnecessary code out from onCreate() ::

 public void onCreate(Bundle savedInstanceState)
 {
  boolean a = registerOperation();

 super.onCreate(savedInstanceState);

  if(a)
    { 
       setContentView(R.layout.main);

       System.out.println("Already logged in"); }
     }
 public boolean registerOperation()

     {


    SharedPreferences pref = getSharedPreferences("MyKid",MODE_PRIVATE);
    String email = pref.getString("email", null);
    String password = pref.getString("password", null);
    if(email == null && password == null)
    {   return false; //Using these flags you can find out whether to redirect to Registration or Already Logged in Page
        this.stopService(this.getIntent());
        Intent register = new Intent(getBaseContext(), Register.class);
        startActivity(register);
    }
      return true;
     }

Upvotes: 0

PeterGriffin
PeterGriffin

Reputation: 910

You're trying to stop a service when you're running in an Activity. Remove the line

this.stopService(this.getIntent());

Nothing is wrong with the way you're calling your other Activity anyway. It's just that you're trying to stop a service, giving as an option an Intent referring to an Activity - which is probably the Logcat error you should have.

Just leave the Activity as it is and let the stack work as intended if you have no specific reason to really shut the Activity down !

Add this to your Activity :

@Override
protected void onStop() {
    super.onStop();
    finish();
}

That way your Activity will be properly destroyed when you're switching to another one.

Upvotes: 0

Related Questions