在我的应用程序中,我有三个主要活动,名为Home,List和Details
我的第一次导航时间如下Name -> List -> Details
在“详细信息”页面中,当用户选择任何选项时,我再次调用相同的活动以显示新的详细信息。这种情况持续了n次。
在详细信息页面中,我有三个重要按钮1. back, 2. List, 3. Home
详细信息页面中的问题是
1. when List button is clicked i have to move on to the List Activity, and by pressing back button from List Activity i need to show Home page, not Details Page
2. when Home button is clicked i have to move on to the Home Activity, and by pressing back button from Home Activity i need the app to get closed.
3. back ->
a. when the user comes first time to the Details page, by pressing back button he needs to move to List Activity
b. after loading the Details activity again and again, by pressing the back button user can move on to previous Details, here the condition is while starting the activity i should not give finish(), because this causes the data to get loaded of a long time
如何克服上述问题
答案 0 :(得分:1)
现在我们将逐一看到您的问题:
1. when List button is clicked i have to move on to the List Activity, and by pressing back button from List Activity i need to show Home page, not Details Page
现在你可以通过以下方式实现这一目标:
每当您参加任何其他活动时,请勿为finish()
致电home or list activity
。
这样,Home activity
将始终存在于堆栈中。现在,请将startActivity
或Home activity
与List activity
联系,将标记设置为FLAG_ACTIVITY_REORDER_TO_FRONT
,以便将您的活动置于最前面。
2. when Home button is clicked i have to move on to the Home Activity, and by pressing back button from Home Activity i need the app to get closed.
3. back ->
a. when the user comes first time to the Details page, by pressing back button he needs to move to List Activity
b. after loading the Details activity again and again, by pressing the back button user can move on to previous Details, here the condition is while starting the activity i should not give finish(), because this causes the data to get loaded of a long time
再次这可以通过使用与home的startActivity相同来完成,其中标志设置如上。
或强>
从startActivityForResult()
使用details
到任何其他活动。因此,现在当您想要从(全部)Home
个活动返回Details
个活动,并使用setResult
作为例如RESULT_FINISH
和onActivityResult()
检查此result code
,如果您收到此结果代码,请致电finish
进行details
活动。
This way, all your Details activity will get finished.
来自Home
活动。如果按finish()
,您的应用将被关闭(make sure all your activities are calling finish at some point of time.)
,因为您的所有活动都已完成。
修改强>
当你从Home > List > Details
出发时,问题是:
1. when List button is clicked from details activity, i have to move on to the List Activity, and by pressing back button from List Activity i need to show Home page, not Details Page.
如果您没有为任何活动Home > List > Details
调用finish(),这将有效。
现在,您可以多次从详细信息中调用详细信息,为此,我建议,这是一个虚拟代码:
public class Details extends Activity{
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/*
* If user presses back button, handle it like this:
*/
if(requestCode == REQUEST_DETAILS) {
if(resultCode == RESULT_FINISH) {
finish();
}
}
// In this way all your Details Activity will get finished.
}
@Override
public void onBackPressed() {
super.onBackPressed();
/*
* If user presses back button, call following:
*
* set some flag as TRUE, ex.:
*
* flagIsFinishing = true;
*
* setResult(RESULT_FINISH);
* finish()
*/
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* if(flagIsFinishing) {
* setResult(RESULT_FINISH);
* finish();
* }
*
*/
}
@Override
protected void onResume() {
super.onResume();
/*
* On selecting any option call Details activity like
*
* startActivityForResult(intent, REQUEST_DETAILS);
*/
}
}
答案 1 :(得分:0)
我使用OnResume()
在List button is clicked i am calling onBackPressed();
的详细信息页面中,当时我将布尔标志设置为true。
默认情况下,如果Detail Activity is called
的{{1}}得到first time
之前的活动,即back to
。
如果单击List Activity
和列表按钮,布尔值为true,则在Detail activity is called for n number of times
我已调用函数onResume
并获得onBackPressed();