-2

I want to get an intent with an boolean from the second activity to my first activity. But now the application checks the intent and I get a null pointer exception directly, how can I skip the first check that happens and go to the second activity, and from there send it back to the first without getting the exception? I get the exception at :

Boolean backButton=fromStartActivity.getExtras().getBoolean("backPress");

Some of my Main Activity code:

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* Load the view and display it */
        setContentView(R.layout.activity_main);
        Firebase.setAndroidContext(this);

        Intent fromStartActivity = getIntent();
        Boolean backButton=fromStartActivity.getExtras().getBoolean("backPress");

        if(backButton){

            logout();
            Intent back = new Intent(Intent.ACTION_MAIN);
            back.addCategory(Intent.CATEGORY_HOME);
            back.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            //startActivity(back);
            finish();
            System.exit(0);
        }

The second activity:

 @Override
    public void onBackPressed(){
        super.onBackPressed();


        backButton=true;
        Intent fromBack = new Intent(StartActivity.this,MainActivity.class);
        fromBack.putExtra("backPress",backButton);
        startActivity(fromBack);



    }
Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
  • You have put extra directly in intent but trying to access using intent bundle. Don't use ackButton=fromStartActivity.getExtras().getBoolean("backPress"); Use ackButton=fromStartActivity.getBoolean("backPress"); – PEHLAJ Apr 27 '16 at 11:38
  • what is the original problem you are trying to solve? because what you mean to do doesn't sound right. also you can getIntent.getBooleanExtra("backPress",false) to get false in case it doesn't exist. – Ramin Apr 27 '16 at 11:43
  • set flag for getting intent. – Hardik Parmar Apr 27 '16 at 12:21
  • What I want to do is that i want to log out, cut of the authtoken and close the application when user presses back on the second screen. – Anders Jansson Apr 27 '16 at 20:12

3 Answers3

2

You can use

startActivityForResult(Intent, key)

from your first activity

And from logout activity, do

@Override
    public void onBackPressed(){
       setResult(RESULT_OK, code);
       finish();
    }
Jithu
  • 1,478
  • 1
  • 13
  • 21
1

You can apply a check before getting data from intent

if(getIntent().getExtras().getBoolean("backPress")){
Intent fromStartActivity = getIntent();
        Boolean backButton=fromStartActivity.getExtras().getBoolean("backPress");

}else{
//do watever you want to do man
}
Himanshu Shekher Jha
  • 1,334
  • 2
  • 14
  • 27
0

You have put extra directly in intent but trying to access using intent bundle.

Try this.. Replace the following code

ackButton=fromStartActivity.getExtras().getBoolean("backPress"); 

with

ackButton=fromStartActivity.getBoolean("backPress");
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53