更改 Android 用户按下列表中的项目时的默认启动设置
Change Android Default Launch Setting when User Presses an Item in a List
假设我们有屏幕 A。当应用程序首次加载时,用户将被带到屏幕 A。屏幕 A 有一个项目列表,这些项目会导致屏幕 B、C、D 或 E。当用户按下其中一个时在这些项目中,他们被引导到他们选择的任何屏幕。然后当用户关闭应用程序甚至关闭他们的 phone 并稍后重新加载应用程序时,他们将被带到他们首先选择的屏幕而不是屏幕 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);
}
}
});
}
}
用户选择这些选项中的任何一个后,他们将被引导到他们选择的屏幕。当他们离开应用程序并稍后返回时,他们选择的屏幕会加载,而不是带有项目列表的屏幕。如果他们以后想选择另一个屏幕,他们可以访问设置并选择他们喜欢的任何屏幕(我已经在操作栏溢出选项之一中有一个列表视图按钮)。
场景:
如果用户选择 "B",他们将被带到屏幕 "B"。如果他们关闭应用程序,关闭 phone,稍后返回应用程序,将加载屏幕 "B" 而不是屏幕 "A"。
PS:我删除了很多代码并替换了实际代码中的名称,以尽量使其更简单。如果代码有问题,请告诉我。
您的默认值 activity 已在清单中设置,无法更改。您可以做的是将此默认 activity 更改为某种启动器,它将打开 activity A、B 或 C。
要知道要打开哪个activity,可以将最后打开的activity保存在SharedPreferences.
如果需要保存activity的状态,可以使用onSaveInstanceState()
如果我是你,我会使用 SharedPreferences 将 activity 名称作为字符串存储在 onPause 中:
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));
}
我没有测试代码,但我认为它应该可以工作。希望对您有所帮助:)
成功了。结果这个问题已经在这里得到了回答:How to make an android app return to the last open activity when relaunched?
我是如何将它应用到这个案例中的:
首先我们创建 Dispatcher.java 以及以下 class:
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();
}
}
结束部分被排除在答案之外。另一个问题的已发布答案的问题是该人没有在调度员代码中包含 "finish()"。因此,当您尝试按后退按钮退出应用程序时,您将进入一个空白屏幕并且必须再次按后退按钮。
现在,在彼此 activity(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();
}
希望这个回答对以后的人有所帮助。
假设我们有屏幕 A。当应用程序首次加载时,用户将被带到屏幕 A。屏幕 A 有一个项目列表,这些项目会导致屏幕 B、C、D 或 E。当用户按下其中一个时在这些项目中,他们被引导到他们选择的任何屏幕。然后当用户关闭应用程序甚至关闭他们的 phone 并稍后重新加载应用程序时,他们将被带到他们首先选择的屏幕而不是屏幕 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);
}
}
});
}
}
用户选择这些选项中的任何一个后,他们将被引导到他们选择的屏幕。当他们离开应用程序并稍后返回时,他们选择的屏幕会加载,而不是带有项目列表的屏幕。如果他们以后想选择另一个屏幕,他们可以访问设置并选择他们喜欢的任何屏幕(我已经在操作栏溢出选项之一中有一个列表视图按钮)。
场景: 如果用户选择 "B",他们将被带到屏幕 "B"。如果他们关闭应用程序,关闭 phone,稍后返回应用程序,将加载屏幕 "B" 而不是屏幕 "A"。
PS:我删除了很多代码并替换了实际代码中的名称,以尽量使其更简单。如果代码有问题,请告诉我。
您的默认值 activity 已在清单中设置,无法更改。您可以做的是将此默认 activity 更改为某种启动器,它将打开 activity A、B 或 C。
要知道要打开哪个activity,可以将最后打开的activity保存在SharedPreferences.
如果需要保存activity的状态,可以使用onSaveInstanceState()
如果我是你,我会使用 SharedPreferences 将 activity 名称作为字符串存储在 onPause 中:
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));
}
我没有测试代码,但我认为它应该可以工作。希望对您有所帮助:)
成功了。结果这个问题已经在这里得到了回答:How to make an android app return to the last open activity when relaunched?
我是如何将它应用到这个案例中的:
首先我们创建 Dispatcher.java 以及以下 class:
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();
}
}
结束部分被排除在答案之外。另一个问题的已发布答案的问题是该人没有在调度员代码中包含 "finish()"。因此,当您尝试按后退按钮退出应用程序时,您将进入一个空白屏幕并且必须再次按后退按钮。
现在,在彼此 activity(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();
}
希望这个回答对以后的人有所帮助。