0

在我的应用程序中,我有三个主要活动,分别命名为 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

如何克服上述问题

4

2 回答 2

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将始终存在于堆栈中。所以现在,呼吁startActivityHome 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()fromdetails到任何其他活动。因此,现在当您想Home从(所有)Details活动返回活动时,并使用setResultas ex.:RESULT_FINISHonActivityResult()检查此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.

如果您不为任何活动调用 finish() ,这将起作用Home > List > Details

现在,您可以多次从 Details 调用 Details,为此,我建议,这是一个虚拟代码:

    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);
         */
    }
}
于 2012-09-12T09:20:04.730 回答
0

我通过使用标志解决了这个问题OnResume()

在详细信息页面List button is clicked i am calling onBackPressed();中,当时我将布尔标志设置为真。

默认情况下,如果Detail Activity is calledfirst time获取back to上一个活动,即List Activity

如果Detail activity is called for n number of times单击 和 List 按钮,布尔值为 true,onResume则我调用了该函数onBackPressed();,它得到moved to the List Activity

于 2012-09-12T11:16:30.437 回答