简体   繁体   中英

How to finish Main Activity onBackPressed?

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

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

It can be done in so many ways...

In your Main Activity:

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

In your Settings Activity:

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

Or in Manifest file use android:noHistory="true"

   <activity android:name=".Settings"
   android:noHistory="true"/>

try like this

{//.............
 Intent i  = new Intent (this, Settings.class);
 startActivityForResult(i, 123);
//............

}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK&&requestCode==123){
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }
    }

pls try launch mode in your manifest file

<activity
        android:name="com.xyz.MainActivity"
        android:launchMode="singleInstance"

        >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM