假设我们有屏幕A.当应用程序首次加载时,用户将被带到屏幕A.屏幕A有一个项目列表,指向屏幕B,C,D或E.按下其中一个项目,他们被引导到他们选择的任何一个屏幕。然后,当用户关闭应用程序或甚至关闭他们的手机并稍后重新加载应用程序时,他们将被带到他们首先选择的屏幕而不是屏幕A.我想知道我将如何能够做到这一点。我将提供一个更实用的代码示例:
//Default Launcher
public class ScreenA extends Activity {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectactivity);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.listView);
// Defined Array values to show in ListView
String[] values = new String[] { "B","C","D", "E"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int itemPosition = position;
String itemValue = (String) listView.getItemAtPosition(position);
if (itemValue.equals("B")){
finish();
Intent nextScreen = new Intent(getApplicationContext(), B.class);
startActivity(nextScreen);
} else if (itemValue.equals("C")){
finish();
Intent nextScreen = new Intent(getApplicationContext(), B.class);
startActivity(nextScreen);
} else if (itemValue.equals("D")){
finish();
Intent nextScreen = new Intent(getApplicationContext(), D.class);
startActivity(nextScreen);
} else if (itemValue.equals("E")){
finish();
Intent nextScreen = new Intent(getApplicationContext(), E.class);
startActivity(nextScreen);
}
}
});
}
}
用户选择其中任何一个选项后,会将其引导至他们选择的屏幕。当他们离开应用程序并稍后返回时,他们选择的屏幕会加载而不是带有项目列表的屏幕。如果他们想稍后选择其他屏幕,他们可以访问设置并选择他们喜欢的任何一个屏幕(我已经在其中一个操作栏溢出选项中有一个列表视图按钮)。
方案: 如果用户选择&#34; B&#34;,他们将被带到Screen&#34; B&#34;。如果他们关闭了应用程序,请关闭手机,稍后再返回应用程序,屏幕&#34; B&#34;将加载而不是屏幕&#34; A&#34;。
PS:我删除了很多代码,并从实际代码中替换了名称,以使其更简单。如果代码中存在问题,请告诉我。
答案 0 :(得分:0)
您的默认活动是在清单中设置的,无法更改。您可以做的是将此默认活动更改为某种启动器,它将打开活动A,B或C.
要知道要打开哪个活动,您可以在SharedPreferences.
如果您需要保存活动的状态,可以使用onSaveInstanceState()
答案 1 :(得分:0)
如果我是你,我会使用SharedPreferences将活动名称存储为onPause中的String:
Editor edit = prefs.edit();
edit.putString("act", "activityToStart");
edit.commit();
在你的onCreate中你可以做到:
String activityToStart = "";
if(prefs.hasString("act")) {
activityToStart = prefs.getString("act");
}
if(activityToStart.equals("B") {
startActivity(new Intent(A.this, B.class));
}
我没有测试代码,但我认为它应该可行。希望它有所帮助:)
答案 2 :(得分:0)
让它发挥作用。原来这个问题已在这里得到解答:How to make an android app return to the last open activity when relaunched?
我如何将它应用于此案例:
首先,我们创建Dispatcher.java以及以下类:
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class Dispatcher extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Class<?> activityClass;
try {
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(
prefs.getString("lastActivity", A.class.getName())); //A.class is the DEFAULT LAUNCHER activity.
} catch(ClassNotFoundException ex) {
activityClass = A.class;
}
startActivity(new Intent(this, activityClass));
finish();
}
}
完成部分被排除在答案之外。对其他问题的回答问题是,该人不包括&#34; finish()&#34;在调度程序代码中。因此,当您尝试按后退按钮退出应用程序时,您将进入空白屏幕并再次按下。
现在,在每个其他活动(B,C,D和E)中你都放了这个:
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
希望这个答案能够在将来帮助人们。