I have three activities in my application ... I use an intent to navigate between the activities.
My third activity contains a method that fetch data from a web service.
When I press back button on the 3rd activity I go back to the second activity. But when I again come to 3rd activity from the second activity it starts downloading data again.
Please help me!
I am using handler in my 3rd screen..
Second activity uses following code
Intent intent=new Intent(Screen2.this,Screen3.class);
startActivity(intent);
Thanks in advance guys.
Try this link: Android Intents - Tutorial.
Using this tutorial
startActivityForResult();
Make use of savedInstance bundle....
Get more info from
Saving Android Activity state using Save Instance State
First finish the second activity moving to the third activity.
From third activity call the second activity
Related
I have two activities in my app, FirstActivity and SecondActivity. FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
One of the thoughts I had on how best to accomplish this would be to open SecondActivity on top of FirstActivity, and then remove SecondActivity, when the user wishes to go back to FirstActivity. That way FirstActivity doesn't have to reload because it was loaded the whole time, merely hidden behind SecondActivity.
However I'm not really sure how to best accomplish this task. I couldn't figure out how to open an activity on top of another activity. Any help on how best to do this would be greatly appreciated, thanks!
FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
I recommend that you start with the 2nd activity directly, and from there you begin an async task to download whatever you need from the internet. When the async task is done, you simply switch to the first activity, which uses the things you downloaded. If you have few things to pass over, consider putting the downloaded info as an extra to the activity switch intent.
Alternatively, look into fragments:
http://developer.android.com/guide/components/fragments.html
Have your two current activities be two fragments. For the main activity, set the 2nd fragment as active at the start of the app. When the download is finished, switch to the 1st fragment.
i am trying to get the intent,which activity had started
the intent, i am trying facebook integration with my app,in which i
want to know who have called my activity is the call from facebook or
withen my app, suppose i have 3 activity a,b & c, a call b c call b
facebook calls b here in activity i want to detect who have started
activity b how to get this, i googled it but no use please share yours
views on this or any refrence thank you
you should maintain a variable for an activity and send it with intent as extra along with intent, and check when you start activity and check that common extra variable.
Like Intent.putExtra("From", "a");
and get data and check it from where it comes
For deep linking, why not just use an Activity class that's specific to Facebook?
Since you're configuring the class name in your app settings, just use a class that's something like: FacebookDeepLinkActivity, and from there, you can start another activity in your app if you want. This way, you know exactly which links came from Facebook.
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.
I have a problem cause when I go to different activities using startActivity function,
they always get created from scratch. Even if activity A was visited before, when I go to activity B and then A again, activity A is created again.
The problem is with back button, cause if I go to Activity A then B then A and then B,
in order to close the application I have to press back button 4 times.
I guess that it shouldn't act like it and user should be able to go to activity A when first pressed back button and the second press should close the application.
How to solve this issue?
Greetings
If you have activity transitions like:
Activity A -> Activity B
Activity B -> Activity A
and you want the user to go back to the same instance of Activity A in this case, maybe you just need to call finish() in Activity B after you call startActivity() for Activity A?
If this isn't helpful, please give us more information about what you are trying to do.
make sure you implement onSaveInstanceState and be prepared to restore your activity from a Bundle in onCreate. that's how you re-establish where you were when you return to an activity.
add launcheMode="singleTask" to your activity in the manifest
You need to set FLAG_ACTIVITY_SINGLE_TOP to your intent for launching activity A. Doing so will cause your previously created activity to re-use. Make sure you do handle your afterwards intents in onNewIntent method. For more info.
You need to set the flag FLAG_ACTIVITY_REORDER_TO_FRONT when you start activity A from B or vice versa, like
i = new Intent("....ActivityAorB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I've tried solutions proposed so far, however they didn't do it for me.
What did however, is using flag FLAG_ACTIVITY_CLEAR_TOP while starting activities.
Thanks for pointing me in the right direction though.
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.