What is the point of super.onStop()? - java

I started learning how to create an app on Android. I have a bit of knowledge on Java already, and now I'm trying some of the activity events.
I know how to use onCreate or onCreateOptionsMenu, and now I'm testing out onStop:
#Override
public void onStop(){
//a function that simply creates and return an AlertDialog.Builder object
displayPopup("Event", "Stopped", "OK").show();
}
I thought this would work since there's no error at compile time. But when I try to exit the app, I expect a popup dialog would show up but instead the app just crashed. It turns out that I need one extra line to make it work:
super.onStop();
It doesn't do anything (at least I can't see anything changed) and it's kind of useless, but without this line the app just keeps crashing.
Any help would be great, thanks.

It calls the onStop() method in the parent Activity class. When you look at the source code, you'll see that it does some internal housekeeping.
protected void onStop() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
getApplication().dispatchActivityStopped(this);
mTranslucentCallback = null;
mCalled = true;
}

Your custom Activity extends the type Activity. The onStop() method is part of the super class activity. If you don't call super.onStop() the implementation on the Activity class is never called, and only your implementation is. This implies crashes since the onStop() method in the Activity classperforms clean ups that must be called.
The android reference on onStop() : http://developer.android.com/reference/android/app/Activity.html#onStop()

Your class is an activity subclass (extends Activity), witch mean that you must call super method of the activity mother class for more information about details of super.onstop() you can check the source code

Related

How to check/access a different activity's lifecycle from the current activity?

I want to access the activity lifecycle method of a different activity from the present one... Can i do that? for example i have 2 activities A and B. I want to access the onStop method of activity A from activity B. can i do that? i'm trying to check the online of a user in my app which has multiple activities so i want to write code which is like = If onStop/onDestroy method of both the activities are called show that the user is offline... The code im using is
#Override
public void onStart(){
super.onStart();
mDatabaseReference.child("Online").setValue(true);
}
#Override
public void onStop(){
super.onStop();
mDatabaseReference.child("Online").setValue(false);
}
Can someone please help me out
Use Application.ActivityLifecycleCallbacks in your Application class. This way you just need to register your activities to the callbacks and from the application class only you can track down wheather any activity is present or not.
For more info please refer to this answer
To set the value You can use SharedPreferences. Declare the instance of sharedpreference at application level.
In Activity A and B you can set the value in onStop(), onDestroy() and onStart() block.

Call finish() or not?

I am trying to fetch some API data to my app and I have two activities. The first one is a Splash Screen (like those used by google while your app gets loaded) and I want to know where to call finish() to end the activity.
public class SplashScreen extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AsyncDataFetch fetch = new AsyncDataFetch();
fetch.setContext(this);
fetch.execute();
}
}
I have moved all my code in my AsyncTask so I don't block the ui thread and now I can't call finish() there, or I don't know how.
Why would I call finish for my app there instead of calling it on my activity you might ask... It is because it generates some sort of glitch if I do this, because my API fetch takes about 1 second and showing and closing this activity takes less.
So, where should I call finish() and how?
You should call finish() in onPostExecute of Async Task after getting result from doInBackground().
If your activity you wanna finish is a splash srceen, you should put finish() at postExecute(), which should be overriden in your asyncsTask.

Using Espresso to stub Intents started during the onCreate of the tested activity

I am testing an activity that starts another activity during its onCreate(). This second activity is started with startActivityForResult(), and the main activity then waits for onActivityResult().
I'm trying to use Espresso to test this, attempting to stub the second activity with intending(), and verify it occurred using intended().
It appears though that espresso-intents isn't designed to work with intents launched from within the onCreate() method (see the warning in the last paragraphs here).
Has anyone managed to stub an Intent started from within onCreate(), and if so, how?
I was able to get this working for myself by using the following Kotlin code:
#Rule #JvmField
val activityRule: IntentsTestRule<MainActivity> =
object : IntentsTestRule<MainActivity>(MainActivity::class.java, true, false) {
override fun beforeActivityLaunched() {
super.beforeActivityLaunched()
Intents.init()
intending(hasComponent(LaunchedFromOnCreateActivity::class.java.name)).respondWith(Instrumentation.ActivityResult(RESULT_OK, null))
}
override fun afterActivityLaunched() {
Intents.release()
super.afterActivityLaunched()
}
}
The general idea is that since the lifecycle stuff happens in between beforeActivityLaunched and afterActivityLaunched, you'll need to set up your intending there. That said, this doesn't make it possible to do intended testing.

How can I get the context in the finish() method?

I'm just rtying to display an offerwall when someone is leaving the application, so I have placed the finish() method:
#Override
public void finish() {
super.finish();
MobileCore.init(this.getContext(), "xxx", MobileCore.LOG_TYPE.DEBUG, MobileCore.AD_UNITS.OFFERWALL);
MobileCore.setStickeezReadyListener(new OnReadyListener() {
#Override
public void onReady(MobileCore.AD_UNITS adUnit) {
if (adUnit == MobileCore.AD_UNITS.OFERWALL) {
MobileCore.showOferWall(getActivity());
}
}
});
}
But I have problems. First of all with this code this.getContext() and also with this getActivity()
I know that I can not access the activity this way, but I'm extremely confused at the moment. I know that I'm missing a very small part here. Can you give me a push?
Activity class extends Context so actually Activity is instance of Context, in onFinish the system is trying to destroy the Activity so there must be no jobs still working related to this Activity, if you still need a Context you can use this.getApplicationContext.
To call the outer class in a nested class (in your case an anonymous class) use the class name of the outer one:
YourOuterClass.this
Your activity's context will remain valid until you call super.finish(). So don't call it until you're done.
If you can't do that, use the application context.

Android - Catching onPause without being the active Activity

Is there any way to catch onPause() in a java class that is Not an activity? I have native code with a Java-helper class that is used in other apps, and whenever those apps enters background I want to prepare for it too. How can I solve this? Is it enough to have my Java class extend some class to get the onPause(), even though its not registered as an activity in manifest etc?
Create a public method named onPause() or prepare() or whateverSuitYou() in your helper class, and invoke it from the onPause() of your Activity.
Today we can use registerActivityLifecycleCallbacks() of the Application class and provide onActivityPaused().
Example:
AppContext.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
#Override
public void onActivityPaused(Activity activity) {
//handle here
}
}

Categories

Resources