Slava
Slava

Reputation: 439

onBackPressed not calls after recreate()

Problem description:

My App has Main activity and Setting Activity. After each closing Settings Activity, Main Activity recreates.

Main Activity can be closed (App exit) by finish() in two places: Home button in Action Bar and Back Button (hardware or popup bar).

Home button works without problem, the Back button works at the start, but if a user was in Settings Activity, closed it and return to Main Activity, onBackPressed() function not called anymore.

If I delete recreation the Back button works fine, just like Home button.

So if somebody know why recreate() messing up with Back button?

Thanks.

Parts of relevant code:

Main Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        // Open Settings Activity
        case R.id.menuSettings:

            Intent intent = new Intent(this, SettingsActivity.class);
            startActivityForResult(intent, REQUEST_SETTINGS);

            return true;

        // Close the App
        case android.R.id.home:

            finish();

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != RESULT_OK) {

        return;
    }

    switch (requestCode) {

        case REQUEST_SETTINGS:

            // Recreate activity
            recreate();

            break;
    }
}

// Close App on Back Button Click
@Override
public void onBackPressed() {
    super.onBackPressed();

    finish();
}

}

Upvotes: 5

Views: 955

Answers (3)

Ahamadullah Saikat
Ahamadullah Saikat

Reputation: 4644

simply replace recreate() method with

finish();
startActivity(getIntent());

& see the magic. You don't need to set any bundle and intent newly.

Upvotes: 1

Bart Burg
Bart Burg

Reputation: 4924

Your finish might not work as expected because you do super.onBackPressed().

Another note, finish doesn't close your app, it only finishes the Activity. If there is another activity on the backstack, it will open that one. Might be this is just fine in your application logic, but keep it in mind.

Something else: I can't reproduce your problem, so your problem might be somewhere else. This is the code I used to reproduce your problem:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recreate();
            }
        });
    }

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

I only just realised this is an old question. If you still have it, please provide the full code of MainActivity and I will update the answer so we could possibly help out others.

Upvotes: 0

Mir-Ismaili
Mir-Ismaili

Reputation: 17184

I found a solution ...

It seems somethings need to be completed before calling recreate() method. I had called it inside onResume and you called inside onActivityResult method.

So I dedicated a little respite to it to be completed its processes (100 milliseconds worked well for my case).

This is my solution:

// Use this instead of calling recreate() directly. 
// This will call recreate() after 100 milliseconds, ASYNCHRONOUSLY:
new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                recreate();
            }
        }, 100);

Upvotes: 0

Related Questions