Problem when navigating between activities

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem when navigating between activities

Postby kitty » Tue Dec 08, 2009 7:48 am

Hi all,

I wrote an Android application which has 4 activities, A.java, B.java, C.java, D.java. Activity B is started from A, C is started from B, D is started from C. For suppose when i'm in the 4th activity(Activity D), how to go back to the 2nd (Activity B) or 1st (Activity A)activity. If i press the back button in the Emulator, it is going back one level only. Can any one tell me how to navigate from 4th activity to 2nd or 1st activity.

Thanks in advance,
Kitty
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Postby Droid123 » Tue Dec 08, 2009 9:42 am

goto:

http://developer.android.com/guide/topi ... procthread

under "Activities and Tasks"

You'll have to fiddle around with the backstack of your Application
Droid123
Experienced Developer
Experienced Developer
 
Posts: 77
Joined: Sat Nov 21, 2009 1:21 pm

Postby saamyandroid » Tue Dec 08, 2009 10:15 am

u just started Activity B from A.

In that case A is in top of the Stake Below B.

That Means In Stake

|B|
|A|
---

started Activity C from B.

|C|
|B|
|A|
---

started Activity B from C.

|D|
|C|
|B|
|A|
---


once u press Back button it will automatically Finish current Activity.

So B will be removed from stake .

so C will be in top of Stake.

So u will c C.

like wise others too.


so if u want to move to Activity A from D , then u have to use intent,StartActivity methods.

Intent i = new Intent(this, A.class);
startActivity(i, 1);
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

Postby Johan Degraeve » Tue Dec 08, 2009 10:51 am

that doesn't solve the problem : what if the user presses the 'return' button.

I think it needs to be solved like this, although I haven't tried it yet :

Activity C starts activity D by using 'startActivityForResult'
Activity C also overloads onActivityResult

In activity D, overload 'onStop', this method is called (I'm not sure however) when the user presses the back key.
In that method you use setResult to set the resultCode.
Then call super.onStop() or maybe Finish().
The resultCode can then be verified by C.onActivityResult and C may then decide to finish(), which will result in B being resumed.
This can go a step further, so that A is finally resumed.
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby Johan Degraeve » Wed Dec 09, 2009 10:42 pm

It's a bit easier,

Following the example above, suppose you have 4 classes that extend Activity, A, B, C, D

From A you launch B with
Intent anIntent = new Intent(this,B.class)
.. you can putExtra's
startActivity(anIntent);

From B you launch C with
Intent anIntent = new Intent(this,C.class)
.. you can putExtra's
startActivityForResult(anIntent,aFinalIntegerB);
In B you overload onActivityResult (int requestCode, int resultCode, Intent data)
switch (requestCode) {
case aFinalIntegerB:
if (resultCode == RESULT_OK)
finish();
break;
.. no need to handle other possibility for requestCode, any other value, Activity B will become the active activity.

From C you launch D with
Intent anIntent = new Intent(this,D.class)
.. you can putExtra's
startActivityForResult(anIntent,aFinalIntegerC);
In C you overload onActivityResult (int requestCode, int resultCode, Intent data)
switch (requestCode) {
case aFinalIntegerC:
if (resultCode == RESULT_OK)
finish();
break;
.. no need to handle other possibility for requestCode, any other value, Activity B will resume.

What you do in D, as soon as you want to force a return to A is to call setResult(RESULT_OK) , followed by finish().
C will resume , but there the resultCode will be analysed which results in a call to finish(), that means B will resume, there also the resultCode will be analysed resulting again in a call to finish(), which means A will resume. There's no other activities on the stack. If user presses back key, then A finishes, the application dies.

You can use other values for resultCode, but RESULT_OK is a static final int defined in the Activity class, for this purpose.
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby kitty » Thu Dec 10, 2009 6:45 am

Hi Johan,

Thanks for your reply. You've given a good solution, but i already found a solution for that problem. In this scenario, there is no need to resume B and C activities, my need is to navigate from D to A.

For that thing to happen i simply used finish() method in B and C activities, then those two activities will be popped out from the stack. So, whenever user click back button Activity A will be resumed.

Regards
Kitty
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests