gpt4 book ai didi

java - 从第二个 Activity 打开应用程序

转载 作者:行者123 更新时间:2023-12-01 22:34:55 25 4
gpt4 key购买 nike

我有一个应用程序,它有一个主 Activity ,您有多个选项可供选择,其中输入了不同的 Activity 。我想要做的是,一旦选择了一个选项,如果我关闭并再次打开,应用程序将从第二个 Activity (从所选选项的屏幕)启动。

这是我的主要 Activity ,但不起作用:

公共(public)类 InicioActivity 扩展了 Activity {

Button b1;
Button b2;
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inicio);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
ElegirIdioma();

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if (sharedPreferences.getBoolean("startFromSecondActivity", false))
{
Intent intent = new Intent(this, EspanolActivity.class);
startActivity(intent);
finish();
}
}

private void ElegirIdioma() {

b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("startFromSecondActivity", true);
editor.commit();

Intent aEspanol = new Intent(InicioActivity.this, EspanolActivity.class);
startActivity(aEspanol);
Toast.makeText(v.getContext(), "Bienvenido" , Toast.LENGTH_SHORT).show();
}
});

/*b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent aEnglish= new Intent(InicioActivity.this, EnglishMainActivity.class);
startActivity(aEnglish);
Toast.makeText(v.getContext(), "Bienvenido" , Toast.LENGTH_SHORT).show();
}
});*/

}}

最佳答案

您可以将该选项保存在 SharedPreference 中,并在 MainActivityonCreate 方法中更改保存的值,然后您可以 finish() 你的 MainActivity 并启动 SecondActivity

将其放入您的onCreate方法中:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

if (sharedPreferences.getBoolean("startFromSecondActivity", false))
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
}

并将其放入 ButtononClick 方法中:

    SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("startFromSecondActivity", true);
editor.commit();

关于java - 从第二个 Activity 打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26996517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com