Android prevent restart activity after startActivity - android

I have a problem with startActivity() methods.
Problem Structure
Click Link (example, http://google.com) in some TextView, using LinkMovementMethod
Callback into LinkCatcher class (because some TextView exist almost all activity, so i using outer class)
Post event to MainActivity using Otto (because it prevent startActivity outside of activity problem.)
on MainActivity, intent to Activity B without any Flag, just one extra (link)
When Activity B created, Activity A will destroy and call onCreate(Bundle savedInstanceState) (i insert some Log.e to my Code, definitely it re-call Log code)
Tried Methods
Insert android:launchMode="singleInstance" in MainActivity, Activity B
Insert android:configChanges="orientation|screenSize|keyboardHidden" in MainActivity (it means, orientation is not problem. i already implement onSaveInstanceState in all activity of my Application)
3.remove Step 3. instead of Post event to MainActivity, call startActiivty() in LinkCatcher Class
I tried all methods which i try it. and i don't know why activity is destroy and re-call onCreate (not onResume) even i don't use any finish() methods.

Because that's how Android is designed- at any moment an Activity not currently on screen can be killed. You can't prevent it. What you can do is account for it- you can save off any necessary data in onSaveInstanceState, and restore it in onCreate from the Bundle or in onRestoreInstanceState.

Related

Stop asynctask from other activity

I creating an app using asynctask to download file from server. It has several activities. I start one asynctask from activity A and I want stop it from activity B. How can I archieve it ???
It is possible to call a previous activity's public methods by using typecasting of the getParentActivity() method on your activity B :
((PreviousActivity)getParentActivity()).somePublicMethod();
This only works if you opened Activity B from Activity A. If you want to be able to call that method from all activities, try creating a static class and save an instance of Activity A. This way, wherever you are on the app, the method can be called. Be also wary of null values when doing this.
This is the sample of the static class.
public static class Constants{
public static ActivityA activityAInstance;
}
When you open ActivityA (onCreate method) save it's instance:
Constants.activityAInstance = ActivityA.this;
This part saves the instance of ActivityA to your static class. So whereEver you are on the app, you can access that instance and call it's public methods:
e.g. the app is on ActivityZ all you have to do is import that static class and call ActivityA instance:
Constants.activityAInstance.somePublicMethodToStopTheAsyncTask();
In my opinion , you can set a boolean flag in activity A ,and when you run the asynctask , you should check this flag ever time,if you want to stop ,you just change the flag , so you could change this flag in activity B to stop the asynctask.
you should try this.
I think you can use LocalBroadcastIntent to send a message from activity B to activity A. In method receiver of A, you can call Asynctask.cancel(true). And you should place if(isCanceled()){
break;
}
in your asynctask's loop to stop the task when you call Asynctask.cancel(true).

Refreshing parent activity on going back from current activity

I am trying to refresh the contents of the previous activity when i am going back from current activity to the just previous or parent activity. How to do this?
When you start the child activity the original activity will receive a call to onSaveInstanceState. It can save any information it needs to in the Bundle passed to that call.
When the Parent activity is restarted, the Bundle created by onSaveInstanceState will be passed to the Activities onCreate method which can use the information as necessary to put the Activity back into the desired state.
This Bundle will also be passed to the onRestoreInstanceState() method of the parent activity so you may wish to use the data there instead.
To read more about this in the Android documentation, take a look at this page
If the Parent activity needs to change state as a result of actions in the child Activity it can implement onActivityResult to receive the data passed back by the child activity as described on this page
It is unclear from question what kind of data you have. You can use adapter.notifyDataSetChanged() on Ui thread inside onResume() to refresh data.

Android won't create second instance of activity

I'm calling startActivity from a method in my Application class, but neither the activity's constructor nor its onCreate method is being called. The Application method is being called when a menu option (in the first activity instance) is selected, so the first activity is on top. I don't have a launchMode set for the activity (i.e. it is "standard"), and onNewIntent is not being called.
However, if I copy the .java file (+ rename the class and copy the Activity entry in AndroidManifest.xml), then call startActivity with the new class the second time, it works just fine. I need to create an unknown and possibly high number of these activities, so this is not a solution, especially when you consider all the duplicated code.
How can I force Android to create multiple instances of this activity?

Where is FragmentActivity#onDetachFragment?

Every time I attach a fragment to my activity, I want to register it to receive events from the activity. This is easy, because I can override FragmentActivity#onAttachFragment(Fragment). When the fragment is removed from the activity, I want to unregister it from receiving events. I expected there to be a onDetachFragment event that I could use in a similar manner, but I'm not finding it.
Is there another way to accomplish what I'm trying to do? I'd like to keep the registering/unregistering in the activity, as opposed to moving it to a base fragment class (where I could just use onAttach/onDetach).
its better to use the onStart(), onStop() method from your fragment. Just cast getActivity() to your calling activity class.

MapActivity instance management

I have a MapActivity subclass and I want to preserve the stack, but I can't keep multiple instances of a MapActivity in the same process. So I have come up with 2 schemes to achieve this:
Pass the state of the MapActivity along with any intents it fires and then let the activities that get switched to reconstruct the MapActivity by sending an intent that recreates the activity. Additionally, the MapActivity would be set so that intents only ever create a single instance of this activity at a time. This approach is flawed as there are multiple exit paths from this activity so all of them would need to be changed to support this.
Replace the MapActivity with a mock activity that does the recreation of the activity in it's onResume() method or something and then the activities you switch to can remain blissfully unaware of this issue. The problem with this approach is I am unaware about how I should go about creating this mock activity and also fire an intent to start the activity I want to switch to.
So my question is this is there a better way to do this and, if not, how would I go about doing option 2, if it is possible?
EDIT: One possible way to do option 2 is to make the mock activity a waypoint that starts the target activity for you in it's onCreate(). But then one just has to be careful that if the onCreate() gets called again because the activity is being reconstructed, that one doesn't start the target activity again. This can be done by checking that savedInstanceState is null.
You should use SingleInstance attribute in the manifest file, this will bring the earlier launched instance to the top of the backstack
<activity android:launchMode="singleInstance"/>

Categories

Resources