Starting Activities in Android - 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).
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.

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.

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

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.

Related

How to kill particular activity from task?

In my application I have 6 activities like 1->2->3->4->5->6. In 6th activity user can choose any activity(I am launching with intent). Means he can choose 1 or 2 or 3 or 4 or 5. Now when I press back button it is coming back to 6th activity again(which I do not want).
ex:
I have executed like this 1->2->3->4->5->6. Now I am in 6th activity. Here I have button, when I press it, it launched 4th activity. My code is like this on button click.
Intent intent = new Intent(MainScreen.this, UniversitiesScreen.class);
startActivity(intent);
So now I am in 4th activity. When I press back button it taking me to 6th activity(which I came from) instead 3rd activity(what I am expecting).
Can any one help me to handle this type of navigation.
Simple answer is edit your onBackPressed method in every activity...
For example, Write in 4th activity like this
#override
public void onBackPressed(){
Intent in = new Intent(this, ThirdActivity.class);
startActivty(in);
finish();
}
Please check the context that you are passing to intent. Every activity is different so pass the activity context from where you are calling another activity.
If my guess is true Mainscreen.this is first activity, and if you call fifth activity from fourth activity, give intent as
Intent in = new Intent(FourthActivity.this,FifthActivity.class);
startActivity(in);
finish();
And you are not saving any data in backstack and you are going back manually...
The best way is to use the NavUtils.navigateUpFromSameTask(this) function in your activity. For this you need to define the parent activity in the manifest for each activity so when you are in the 4th activity then the parent activity is the 3rd activity.Then when you click on back or navigate up you end up in the parent activity i.e. from 4th to the 3rd.
Take a look at this
http://developer.android.com/training/implementing-navigation/ancestral.html
Look at the manifest and then the Navigate up to parent task section
This will also help in returning to the same state of the 3rd activity from the 4th activity.

How will I permanently exit my Android app using onBackPressed from my last Activity

I have 8 Activities in my Android app and I want:
1)Every time I press Back button during my first 7 Activities to go back to my previous Activity(Act1< Act2< Act3< Act4< Act5< Act6< Act7) BUT
2)ONLY when I am in the 8th Activity I want to definitely exit my Android app and go to my phone's Home Screen.I try to do it by overriding onBackPressed method in my 8th Activity (Phone Home Screen<-Act8)
I found an Android implementation in which I insert finish();in every intent of all my 8 Activities but this is not what I want since this way I can't go back to the previous Activity whenever I want(with finish(); every current Activity is removed from back stack).
How will I do it please?
My code so far in my 8th Activity is:
#Override
public void onBackPressed()
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
Another way: create a 9th Activity and call it FinishAllActivity or something like that. Make this activity call finish() and then return in its onCreate().
In onBackPressed() in Activity 8, start FinishAllActivity using the FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK flags (see this question for more details). Activities 1-8 will be removed from the stack, then the 9th Activity will start and immediately terminate and your task stack is clear. When you reopen the app it should start from Activity 1.
The advantage of doing it this way is that you don't have to modify Activities 1-7.
Add a public static boolean to one of your classes that indicates the app is exiting. Set this boolean in activity 8 when you want the app to finish, and have all of your other activities check it in their onResume() and finish immediately if it is true. Make sure the first activity clears it before finishing, or it may still be set the next time the app runs. (Android doesn't necessarily discard the VM when your last activity finishes, so the class and its static members may be reused next time.)
Note that this is the simple way, not the "Android way." Global variables are generally frowned upon, for reasons you can Google. The "correct" way to do this would be to start each activity for result and return a result to onActivityResult(...) that indicates whether the app is exiting.
You can implement a broadcast receiver and have each of your Activities that you want to close call finish() when they receive the broadcast (which will be sent from your last activity). I would imagine you'd need to have your broadcast receiver class be either an anonymous inner class or a private class within your activity(s) so that you can easily access your enclosing Activity's finish method.
Here's a good example of broadcast receivers:
http://www.tutorialspoint.com/android/android_broadcast_receivers.htm
Look at the custom intents section.
Doing it this way is a loosely coupled way to implement what you are looking to do.
use FLAG_ACTIVITY_CLEAR_TOP Flag in your intent like below example.
Intent intent = new Intent(getApplicationContext(),FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
in your first activity check below condition.
if (getIntent().getBooleanExtra("EXIT", false)) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
}
here FLAG_ACTIVITY_CLEAR_TOP work like below example
consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
so here you have to call D is your last activity and A is your first activity.
This way you are finishing your 8th Activity returning to your 7th Activity and same time you are like emulating a pressing of Home button on a device. When you rerun your app it will appear with 7th Activity on a screen. If you wish to see the 8th Activity in this case then just remove the finish() method. If you wish next time your app to start with 1st Activity then you should finish all the activities from 8 to 2nd but not the 1st. Also you could launch your 1st Activity adding a FLAG NEW_TASK or some other flags.
UPDATE
My advise (for the quick result without changing the workflow) is to use startActivityForResult() to start all activities in chain. When user exits the app just return a special parameter using setActivityResult() to get all nested activities know about user's choice making all nested Activities to run finish(). This way all your 8 activities will be finished properly.

From within an android activity test, how do I finish another activity that my main activity started?

I am writing an activity test for an activity we wrote with 3 buttons. 2 of these buttons start other activities.
I can write a test that simulates a button push and then checks if the desired activity is running, but I can't move back from that second activity. The second activity stays at the front and prevents the other tests, that assume the first activity is running, from working properly. They just kind of freeze.
I have a reference to the first activity, but it is the second activity I need to I guess call finish() on. Is there a way to do this?
EDIT: I added some actual source code illustrating my problem in this gist: https://gist.github.com/3076103
It is specifically about testing activities. In the production code everything is fine.
You should probably use http://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor.html to get a reference of the second activity or you can block the second activity from being launched(Still you are guranteed that the call to start the second activity infact had reached till the framework).
You need a way for your activities to communicate with one another, so that one activity can tell the other to finish. There are several ways you can accomplish this. One method is to create a service within my application; my "second" activities would connect to this service to register a way to receive messages, and my primary activity would connect in order to provide them.
In Activity1 add the following to start Activity2
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
In Activity2 add the following to start Activity1 and finish Activity2
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
For more details: http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
If you start the activity using startActivityForResult, then you can close the activity from parent using finishActivity(int requestCode).
To start the activity :
startActivityForResult(new Intent(...), 123123 /*requestCode*/);
And when you want to finish that activity (from caller), use :
finishActivity(123123 /*requestCode*/)
Also there is a way to find, whether child activity is finished or not. But you can track this only when child activity calls finish() for self. To receive the child finish request from the child, you need to override the finishFromChild() method in parent activity.

How can i manage my activities and finish them properly?

I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.
On moving from one activity to the other I am using the below code:
Intent intent=new Intent(current.this,next.class);
startActivityForResult(intent, 0);
And now if I want to move back on the earlier activity I used the code as:
Intent start = new Intent(current.this,next.class);
startActivity(start);
finishActivity(0);
Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?
The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.
If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.
You are starting activities for a result but how I understand you you will never return to them.
You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.
If you want to move from Activity A to D like going to the start/home screen of you app you do the following:
Intent goBackToA = new Intent(context, StdActivity.class);
goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goBackToA);
The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .
whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,
Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(nextpage);
this.finish();

OnClick restartActivity?

How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.

Categories

Resources