MDMalik
MDMalik

Reputation: 4001

Starting Activities in Android

I'm a newbie in Android after Googling I found if you want to call another activity you have to use
Intent intent = new Intent(getBaseContext(), Activity_Name.class);
//if Any Extra
//intent.putExtra("", "");
intent.putExtra("EmpID", EmpID);
startActivity(intent); `

Now this is a standard approach but if you refer to the image. I'm lopping almost 20 times in the ListView (Activity1). Work Flow

Now what I belive that this keeps adding again and again as my app crashes later on without any proper reason and not at any particular location.

I have disable the back button on the DataCollection Screens (Activity 2, 3,4)

Hence its a pure Waterfall approach. Any suggestion. Should I add finish(); line in the last screen

Intent intent = new Intent(getBaseContext(), Activity_Name.class);
//if Any Extra
//intent.putExtra("", "");
intent.putExtra("EmpID", EmpID);
startActivity(intent);
finish(); `

So that it dies and goes to the previous and that dies and goes to the previous. And later landing on the ListView.

Would that be a good approach. Or is there anything where I can just call another activity like this but the system should forget all the previous activity information.

Upvotes: 0

Views: 91

Answers (3)

Neil Townsend
Neil Townsend

Reputation: 6084

You are probably better of using startActivityForResult rather startActivity. Then, when you call an activity you know that when it finishes it will return to the Activity which called it (it will come into onActivityResult). This way you can achieve:

Activty 1 -> Activity 2 -> Activity 1 -> Activity 3 -> Activity 1 etc

or

Activty 1 -> Activity 2 -> Activity 3 -> Activity 2 -> Activty 1 etc

In both cases, you don't can keep "passing through" phases of an activity invisible, they can just receive the "activity you called" trigger and pass on to the next activity.

Upvotes: 1

Johannes Staehlin
Johannes Staehlin

Reputation: 3720

Or is there anything where I can just call another activity like this but the system should forget all the previous activity information

Have a look at Intent Flags, e.g.FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP.

Upvotes: 1

petey
petey

Reputation: 17170

If you dont want the user to go backwards, finish the current activity just after starting a new one.

after

startActivity(intent); 

call

finish();

If you'd like to clear the activity stack, when going to a particiluar activity find it your manifest (like "Activity 1") and add the attribute android:clearTaskOnLaunch="true"

more info here : http://developer.android.com/guide/topics/manifest/activity-element.html#clear

Upvotes: 1

Related Questions