0

i have four activities.

like (1) (2) (3) (4).

(1) is first activity or main activity.

i have a bottom bar for these activities. if i click on (2) i want to open second activity. after this if i click on (3) i want to open third activity. and if i click on (4) i want to open fourth activity.

after this if i click on (1) i want to display first activity. without finishing any other activity or again open (1) activity.

a image for easy understanding is attached..

enter image description here

please open this image in new tab to view clearly. i want to do this without using tabhost. can it done by using activity group.

suggest any example or tutorial for this.

thanks Rock Brown

3 Answers3

2

This use case is already built into the platform:

In your AndroidManifest.xml there is an activity element for each activity. In the activity element for 1 set the launchMode:

android:launchMode="singleTask"

This causes the platform to only ever launch one instance of Activity 1 in the app's task (which is a stack of activities.)

When you start activity 1, 2, 3, or 4 set the Intent's flags to include FLAG_ACTIVITY_REORDER_TO_FRONT:

intent.setFlags(intent.getFlags() | FLAG_ACTIVITY_REORDER_TO_FRONT);

This causes the platform to bring any existing instances of 1, 2, 3, or 4 to the top of the activity stack, rather than creating a new activity and placing it on top of the stack.

nmr
  • 16,625
  • 10
  • 53
  • 67
0

An activity can be closed at any moment by the os if a memory pressure append and your activity is in the background.

Pascal Piché
  • 600
  • 2
  • 13
0

I agree with Aashish that you probably want to look at the TabHost tutorial.

But, yes, you can do it without TabHost.

Activity (1) will get launched first, assuming it is the launch activity as defined in your manifest. You can switch to one of the other activities at any time using Context.startActivity(Intent).

You can even do this before (1) is displayed based on state preserved in a bundle or saved to user prefernces (i.e. so it restarts on the same activity that was last used). In a case like this you don't want to the current activity to be on the backstack so you call Activity.finish() after launching the next activity, so now you have.

public void showNextActivity() { Intent intent = new Intent(this, NextActivity.class); startActivity(intent); finish(); // so ThisActivity isn't in the backstack. }

You should probably have some code that is shared between your activities to manage the widgets that are used to change between activities (e.g. the Button's) and to handle the switching. Note that, even if each activity has its own resources, you can use the same r esource ID's on each - so the Button that selects activity 2 has ID 'act2' on each activity. This makes sharing code easier.

Tom
  • 17,103
  • 8
  • 67
  • 75