eternalmatt
eternalmatt

Reputation: 3564

How to properly switch Activities in Android?

I'm really not sure how to ask this question but I need a way to allow the user to switch between lists by clicking on them and pressing the 'back' button works naturally. For instance, I present the users with names of people, they click a name, and a new list appears containing messages from that person.

I'm doing it really poorly right now by having a single ListActivity and I use onListItemClick to listen for any click on a list. Then I call setListAdapter(someArrayList) each time the list changes.

Surely there is a much more intuitive solution than this. Could someone point me toward some tutorials on how to do this?

Upvotes: 0

Views: 472

Answers (1)

Matthew
Matthew

Reputation: 44919

You basically need multiple activities, each having their own lists. Then you can start other activities by doing:

Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);

Edit - parameters for activities can be attached to the Intent before calling startActivity:

intent.putExtra("person", "Bob Smith");

and later retrieved in the next activity using:

String person = intent.getStringExtra("person");

So you would have a PeopleActivity, a MessagesActivity, etc. and inside that activity the list would only display items pertaining to that type of information.

You might want to check out Activities and Activity and Task Guidelines to get a better picture of what's going on when you start new activities.

Upvotes: 4

Related Questions