How to finish Main Activity onBackPressed?

Sid picture Sid · Feb 16, 2016 · Viewed 11.8k times · Source

I am setting themes to my application. Themes are getting set well. I have two activities on is main activity and other is settings activity.

When I am setting theme from settings activity, I have called Main Activity onBackPressed of settings activity. So the changed theme appears.

But if I press back of main activity it again shows the main activity with previous theme and at second time closes the application.

It should not show previous theme again. I want to finish Main activity if back pressed at once.

I tried calling finish onBackPressed of main activity but it did not work.

What to do?

Main Activity

public class MainActivity extends AppCompatActivity {

    private NavigationView navigationView;
    private DrawerLayout drawerLayout;
    Toolbar mToolbar;
    private MainFragment mFragment;
    private int no,no1,no2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Theme.onActivityCreateSetTheme(this);

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

        DatabaseHelper db = new DatabaseHelper(this);
        db.createDatabase();


        setUpToolbar();
        drawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                drawerLayout.closeDrawers();
                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {

                    case R.id.settings:

                        Intent i = new Intent(getApplicationContext(),Settings.class);
                        startActivity(i);

                }
                return true;
            }
        });
        mFragment = new MainFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.nav_contentframe, mFragment).commit();
    }

    private void setUpToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            mToolbar.setTitle("");
            setSupportActionBar(mToolbar);}
        if (mToolbar != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationIcon(R.drawable.ic_drawer);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    drawerLayout.openDrawer(GravityCompat.START);
                }
            });
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main2, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {


            Intent i = new Intent(this,Settings.class);
            startActivity(i);

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed()

    {
        super.onBackPressed();
        this.finish();
    }
}

EDIT:

If i just finish settings activity, changed theme dose not get applied to main activity. As I have called Theme.onActivityCreateSetTheme(this); ----onCreate of main activity, so for this i have started main activity again from settings activity.

Or how can I apply theme if I finish settings activity?

Thank you..

Answer

Ashish Vora picture Ashish Vora · Feb 16, 2016

Just finish Settings Activity when on back pressed in Settings Activity. So it will go to Main Activity.

So put below code in override method onBackPressed.

@Override
public void onBackPressed() {
    super.onBackPressed();
  Intent i=new Intent(this,MainActivity.class);
  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  finish();
}