[英]How to switch back to my choose language Activity when I press the exit button?
我需要幫助我的清單啟動器活動是 LanguageChooseActivity.java,當用戶選擇語言時,我使用了共享首選項,他關閉 LanguageChooseActivity 並在啟動應用程序后直接進入 mainActivity 但在主活動中我有退出按鈕,當用戶按下退出按鈕時,他應該返回 LanguageChooseActivity 再次選擇語言。 請幫助做到這一點。
這是我的代碼:LanguageChooseActivity
private TextView rusVersion;
private TextView engVersion;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private boolean launchedEng;
private boolean launchedRus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language_choose);
afterChoseLanguage();
rusVersion = findViewById(R.id.rus_version);
rusVersion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rusVersion.isPressed()){
editor = sharedPreferences.edit();
editor.putBoolean("launchedRus", true);
editor.apply();
}
startActivity(new Intent(LanguageChooseActivity.this,RusMainMenuActivity.class));
}
});
engVersion = findViewById(R.id.eng_version);
engVersion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(engVersion.isPressed()){
editor = sharedPreferences.edit();
editor.putBoolean("launchedEng", true);
editor.apply();
}
startActivity(new Intent(LanguageChooseActivity.this,EngMainMenuActivity.class));
}
});
}
public void afterChoseLanguage(){
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
launchedEng = sharedPreferences.getBoolean("launchedEng",false);
launchedRus = sharedPreferences.getBoolean("launchedRus",false);
if(launchedEng){
Intent intent = new Intent(this, EngMainMenuActivity.class);
startActivity(intent);
} else if (launchedRus){
Intent intent = new Intent(this, RusMainMenuActivity.class);
startActivity(intent);
}
}
} ```
and MainMenuActivity:
``` FrameLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dm = getResources().getDisplayMetrics();
MainActivity.scale =(float) dm.widthPixels / 1080f;
MainActivity.dm = dm;
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
layout = new FrameLayout(this);
layout.setBackgroundColor(ResourceColors.colorWhite);
setContentView(layout);
design();
}
private void design(){
Bitmap logoMain = getBitmapFromAsset(this,"logo_Main.png");
logoMain = Bitmap.createScaledBitmap(logoMain,(int)(480*MainActivity.scale),(int)(158*MainActivity.scale), false);
ImageView iLogoMain = new ImageView(this);
iLogoMain.setImageBitmap(logoMain);
iLogoMain.setLayoutParams(new FrameLayout.LayoutParams((int)(480*MainActivity.scale),(int)(158*MainActivity.scale)));
iLogoMain.setX(MainActivity.dm.widthPixels/2 - logoMain.getWidth()/2);
iLogoMain.setY(80*MainActivity.scale);
layout.addView(iLogoMain);
for (int i = 0; i < 20; i++) {
Bitmap iconRus = getBitmapFromAsset(this, "iconMainMenu/ru_" + (i + 1) + ".png");
iconRus = Bitmap.createScaledBitmap(iconRus, (int) (238 * MainActivity.scale), (int) (238 * MainActivity.scale*(iconRus.getHeight()/(float)iconRus.getWidth())), false);
ImageView iIcon = new ImageView(this);
iIcon.setLayoutParams(new FrameLayout.LayoutParams(iconRus.getWidth(), iconRus.getHeight()));
iIcon.setImageBitmap(iconRus);
iIcon.setX(55 * MainActivity.scale + (244 * (i % 4))* MainActivity.scale);
iIcon.setY(390 * MainActivity.scale + (277 * (i / 4))* MainActivity.scale);
iIcon.setId(100+i);
iIcon.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceType")
@Override
public void onClick(View v) {
if (v.getId() == 119) {
}
}
});
layout.addView(iIcon);
}
}
public static Bitmap getBitmapFromAsset(Context context, String filePath) {
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
istr = assetManager.open(filePath);
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
// handle exception
}
return bitmap;
}
}
如果我正確理解了您的問題,您已經在代碼中使用了答案......在 onClick 中有一個方法可以讓您的退出按鈕開始另一個活動。
public void btnExit (View view) {
Intent intent = new Intent(getApplicationContext(), LanguageChooseActivity.class);
startActivity(intent);
}
如果您想根據您來自的屏幕設置某些內容,您可以將額外信息與意圖調用一起傳遞。 只需在 Intent 定義和 startActivity 調用之間插入代碼。
intent.putExtra("STRING_NAME", VALUE_UNDEFINED); // use "" on the value if sending a string else, don't
然后當您的 LanguageChooseActivity 再次啟動時,您可以找到以下信息(此處您需要定義發送的變量類型)
Intent intent = getIntent();
if (intent.getStringExtra("STRING_NAME").equals("VALUE")) {
// or (intent.getIntExtra("STRING_NAME") == 0) etc
// do work
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.