Android activity restart with same Objectreferences - android

i already read the question Activity restart on rotation Android . But it does not really help me.
I've two activities. The Second gets updated from an Async_Task, which holds a few Threads which i cannot just restart.
The Problem i got is, that rotating my Device forces an Activity-restart, which means i loose my Reference to the Async_Task.
Is there any possibility to pass Objektreferences from one activity to another?
Thanks for help!

Make your reference static, or see: http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject

Related

Can somebody clarify it this is a leak?

Please I would like to understand this situation.
If I rotate my Android application a few times and I look into the memory dump I find more than one instance of my Activity.
That looks like a leak.
But if I force garbage collection just before getting the memory dump (by pressing the button in android Studio) I find only one instance of my Activity.
I am very inclined to think I don't have a leak.
Please does anybody have a definitive answer?
Shouldn't helped you the parameters added to your Activity like:
android:configChanges="keyboardHidden|orientation"
After that, you could handle logic of orientation changes by yourself in the onConfigurationChanged method.
Or the second solution (couldn't completely imagine the structure of your project) should be using the launchMode parameter in AndroidManifest.xml

What exactly happens when you fail to use unbind()?

I have an app where I use Butterknife, and recently I found a fragment where I had failed to call unbinder.unbind() in the fragment's onDestroyView(). I fixed the problem but it made me start thinking.
What kind of errors can this cause and why? I don't have a particular error right now but I would like to know what to watch out for in the future, and the website for the library doesn't specify the problems this could cause.
Imagine you have a retained fragment and you have initialized a view using #BindView.
An orientation change happens, which results in destroying activity instance, but not this fragment, because this fragment is a retained fragment, which means, that the field that you have initialized is still there (not null) and is holding a strong reference to the view of the previous activity, which results in leaking of the activity.
Although this might take for some small amount of time (because eventually you are going to perform another ButterKnife.bind() in onViewCreated(), right? But who knows, maybe you won't), still it is better to release resources as soon as you do not need them and let the GC do its job.
I've also thought about this question some time ago and other than this I couldn't come up to another scenario where unbind() would be strongly necessary.

How to tell if a new activity appeared?

I'm doing some black box testing (using UiAutomator 2.0 btw, extending InstrumentationTestCase) and I need to know:
1 - when a new activity is created
2 - to know if it's the first time the activity is created
I need this because there are some tests that I want to apply when a new activity appears but I want this detection to be automatic, not manual.
Prior to Android L there was the UiDevice.getCurrentActivityName() method. However, now it is deprecated (moreover, they don't even ensure it works for previous versions). This also happened with the options to getting the activity though the PackageManager.
As such, I would like to know:
Is it possible to programatically detect a new activity? If so, is is possible to know if it's the first time the activity occurs.
If it's not possible, how should I define an activity according to its UI? How many widgets should change for me to conclude it's a different activity?
Thanks.
EDIT: Just to be clear, I don't want to test what happens when the activity is created, I want to be able to identify if it's the first time this activity occurs in a run.
Here is what I currently implemented for this problem, base on a assumption that the same Activity won't change it's UI View hierarchy dynamically. This is OK to me, seems I only want to distinguish between UI changes, not necessary for Activities.
1.Build a signature to identify between screens
signature1:FrameLayout;ListView;LinearLayout;RelativeLayout;...
signature2:FrameLayout;FrameLayout;FrameLayout;LinearLayout;...
2.Then, use a List to keep screens you already known.
3.For performance, you can use a fixed length for screen signatue.
The idea is refer to this paper
And if adb is ok to you, you can use this command,
adb shell dumpsys activity
This refer to another post here

Reusing the main activity

I have a main activity from where I will be switching from one activity to
another..So i don't want to re initialize every time..After the first time
of creation , the same activity should be called without having to create it
over and over again..how do I do this? #newbie-android
What you want to do is simply start both your activities and switch between them by bringing them to the foreground. You should refer to this question for a possible solution. This documentation might also help.

How to run callback on application launch?

I know Android's Activity model is a bit different from what I usually consider to be an "app".
I want to do something (in this case, check some notifications on a server and show them if available) when my app is "launched". What is a good way to accomplish this?
I likely don't want to do it in an activity's OnCreate, since each activity can be created any number of times - the code would get called more often than necessary.
The app also has multiple entry points - would I have to duplicate the check in each activity?
What I'm thinking of doing is setting up this code inside the Application object, along with a flag that tracks whether it's already been called - and just call it from each Activity's onCreate().
Is there a better or more "proper" way to do this?
The right, Android-approved way to do this is:
Create your own android.app.Application class
Override the onCreate method
In the AndroidManifest.xml, change the android:name attribute of the application element to the name of your class
Now, whenever your app is "started" (any one of your activites is started for the first time and no other instances are alive) onCreate will be called.
You may also find the onTerminate method useful.
Can you just check if the bundle passed to onCreate() is null?
It's not null "If the activity is being re-initialized after previously being shut down..."
There's probably no harm in putting it in onCreate; the Activity is really only destroyed when the OS needs the RAM for something else, not when the user goes to another app.
EDIT: You can also have a Service that runs when the device gets booted up, too. This might be a better option if you also want to check when the app starts, since you'll only have to call context.startService from the Activity to run the check. Just be sure to stop it when it's done if you don't need it to be persistent.

Categories

Resources