我的应用中有一个用于每个活动的图像按钮。我需要在按下图像按钮时转到主屏幕。我正在使用代码
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent=new Intent(SpecificCategoryActivity.this,AreaSelectionActivity.class);
//startActivity(intent);
Intent homeIntent = new Intent(SpecificCategoryActivity.this, AreaSelectionActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
});
但是,当我点击图片按钮后从主屏幕导航时,它应该从它呼叫的地方转到同一页面。
例如,我有Activity1,Activity2,Activity3,Activity4等...... Activity1是主页,我将选择country并转到Activity2然后转到Activity3,依此类推,但是如果我点击Activity4中的Location按钮,它应该转到主页,在更改国家后它必须回到Activity4.And还有一件事是,如果已选择国家/地区应用程序将从Activity2启动。
我的Activity1代码是
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!autoComplete.getText().toString().isEmpty()) {
if (isSelectedText) {
Intent intent = new Intent(CountrySelectionActivity.this, CategoryListActvity.class);
startActivity(intent);
}
else{
editor.clear();
editor.commit(); // commit changes
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CountrySelectionActivity.this);
alertDialogBuilder.setMessage("Please specify the correct country name");
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
}
});
请帮助解决问题。 提前致谢。
答案 0 :(得分:1)
I will give some pseudo code here
In Activity4, locationbutton onclick listener will have the below code
locationbutton .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//save a variable to indicate that activity4 is reached
savePreferences("**pagenavigated**", "4", context); //need to write //savePreferences
Intent intent = new Intent(activity4.this, homepage.class);
startActivity(intent);
});
//We can write this method commonly so all activity can access this
public static void savePreferences(String key, String value, Context context) {
SharedPreferences SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = SharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String loadPreferences(Context context, String key) {
SharedPreferences SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
String userDetails = SharedPreferences.getString(key, "");
return userDetails;
}
public static void removePreference(String key, Context context) {
SharedPreferences SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = SharedPreferences.edit();
editor.remove(key);
editor.commit();
}
public static void removeAllPreferences(Context context) {
SharedPreferences SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = SharedPreferences.edit();
editor.clear();
editor.commit();
}
//In Activity 1, check for sharedpreference variable value and if it is 4, it //should get navigate to 4th screen . To make this happen , after checking //the value, we need to remove the shared value as well
//Activity 1
//In **country dropdown change listener**, check for the value of //**pagenavigated** and if it is 4, start the activity to go for activity 4. //Before that remove the value by using removeAllPreferences method
//So changes will be in onItemSelected
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String navigatedPage = loadPreferences(context,"pagenavigated");
if (navigatedPage.equalsIgnoreCase("4") {
removeAllPreferences(this); //Remove the preference and go to activity 4)
Intent intent = new Intent(homepage.this, activity4.class);
startActivity(intent);
}
Hope that helps!!!
答案 1 :(得分:0)
在这里你可以使用 startActivityForResult(Intent intent,int resultCode);
e.g
Intent intent = new Intent(CountrySelectionActivity.this, CategoryListActvity.class);
intent.putExtra("Select_Location", true);
startActivityForResult(intent,007);
现在在第一次活动中检查意图数据,如果您将“ Select_Location ”标记为true,那么在选择城市后,请调用finish();
e.g。
在活动1的onCreate方法
Intent i=getIntent(); //**i** will be null if Activity is called from another Activity
if(i!=null)
boolean flag=i.getBooleanExtra("Select_Location", false);
//now you have flag which indicates that whether this intent is from 4th activity or not.
让用户选择城市,在onClick方法中只检查我们的标志是否为真。
如果是真的那么只需使用意图并附上所有必需的信息并调用完成。
e.g
Intent resultIntent = new Intent();
resultIntent.putExtra("city_id", <city id>);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();
现在你需要覆盖名为 onActivityResult 的方法(在活动4中)
e.g。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 007 && data != null){
// take out all the parameters from data(which is an object of Intent).
}
}