java 如何完成 onBackPressed 的主要活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35426496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:08:30  来源:igfitidea点击:

How to finish Main Activity onBackPressed?

javaandroidandroid-activityactivity-finishonbackpressed

提问by Sid

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.

当我从设置活动设置主题时,我调用了设置活动的主活动 onBackPressed。这样就出现了改变的主题。

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.

我尝试调用主要活动的完成 onBackPressed 但它没有用。

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.

如果我刚刚完成设置活动,更改的主题不会应用于主要活动。正如我所说的 Theme.onActivityCreateSetTheme(this); ----onCreate 的主要活动,所以为此我从设置活动再次开始了主要活动。

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

或者,如果我完成设置活动,我该如何应用主题?

Thank you..

谢谢..

回答by Ashish Vora

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.

所以把下面的代码放在覆盖方法 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();
}

回答by Asim Roy

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"

或者在 Manifest 文件中使用android:noHistory="true"

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

回答by sanemars

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

回答by Manish

pls try launch mode in your manifest file

请在清单文件中尝试启动模式

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

        >