Michael
Michael

Reputation: 375

Back button causes NullPointerException

I pass parameters from MainActivity to Acivity2 by using

Bundle b = getIntent().getExtras();

Then I have a back button from FinalActivity to Activity2. --> Error occurs

I know it is because this line, because I didn't pass any parameter from FinalActivity to Activity2, but I'm not quiet sure how to fix this.

Bundle b = getIntent().getExtras();

Here is the code for back button. getActionBar().setDisplayHomeAsUpEnabled(true);

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Upvotes: 1

Views: 1196

Answers (1)

codeMagic
codeMagic

Reputation: 44571

If you aren't wanting to pass anything back from FinalActivity then just use something like

if (getIntent() != null)
{
     Bundle b = getIntent().getExtras();
}

After your comment, it sounds like you need some more persistent data then what you get with an Activity. You can check out Storage Options Docs to see what would work best for you. But from what little I know if your needs I would say you could use SharedPreferences if you want the data to be stored even if you leave your app.

If you only need it to persist through the life of your app then you can create a static class and store/ reference the variables there. If you need more help then you will need to provide a more context or examples of what you are trying to do

Upvotes: 4

Related Questions