How to finish Main Activity onBackPressed?
<p>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.</p>

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

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

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

<p>I tried calling finish onBackPressed of main activity but it did not work.</p>

<p>What to do?</p>

<p>Main Activity </p>

<pre><code>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();
    }
}
</code></pre>

<p>EDIT:</p>

<p>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.</p>

<p>Or how can  I apply theme if I finish settings activity?</p>

<p>Thank you..</p>