0

I usually navigate between the Activities using Intents

Pictorial representation looks as below::

enter image description here

In Activity - A .......... There is a next button to go to activity B

Photos.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent PhotoIntent=new Intent(a.this,b.class);
                startActivity(PhotoIntent);

            }
        });

In Activity - B .......... There is a next button to go to again activity C

Map.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Intent PhotoIntent=new Intent(b.this,c.class);
                    startActivity(PhotoIntent);

                }
            });
  • Now in Activity C .... there is only one back button
  • If I use it i similarly to above code I can't go to two activities depending on the navigation i came from

Suppose i came from A to C ------ when i use Back button in C go to A

Again when came from B to C ----- when i use Back button in C go to B

There is only one back button in Activity C


[Edit]

Its like Say i came from Activity A to Activity C ----- When i USE the only back button in Activity C it should go to activity A

&

For the same scenario if i came from Activity B to Activity C - - - - When i USE the only back button in Activity C it should go to activity B

------- I am trying to relate a switching case kind of mechanism for Back button in ActivityC


How to Achieve this

Hope i am clear

4

3 回答 3

1
There is only one back button in Activity C

If I understand correctly, Activity C can be started from both Activity A and Activity B. If this is the case, the back button in Activity C should have the following code:

backButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        // Takes you to whichever activity launched C
        finish();
    }
});

When you launch Activity C from Activity A, the stack is .... {Activity A}, {Activity C}. Calling Activity#finish() in Activity C will take you to Activity A. Same scenario unfolds when Activity C is started from Activity B.

Note that if you call finish() in Activity A after launching Activity C, the back button will take you to the Activity that is positioned before Activity A in the stack.

于 2013-10-07T02:20:33.907 回答
1

Do like this

backBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                onBackPressed();

            }
        });

In this way , you have called back button functionality of device Now the OS will decide where to navigate according to the Stack, whether Activity A is present or Activity B in the stack.

于 2013-10-07T02:37:56.870 回答
1

call finish() or onBackPressed() function on back button click event.

于 2013-10-07T06:56:07.693 回答