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
}
}
Related
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.
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.
I starting intent service in onCreate() method in subclass of Application class. But it is called when app is opened the first time. It should not calling on every time the app is opened. It does even not work when I kill the background process. And my observation its working in galaxy note but not in samsung s duos, please help me.
public class MyApp extends Application {
#Override
public void onCreate() {
Toast.makeText(this.getApplicationContext(), "APPLICATION", Toast.LENGTH_LONG).show();
context=this.getApplicationContext();
if (!ContactUtiles.isFileExit()) {
createDatabase(Opration.NOT_FOR_DELTA);
if (DBConstants.LOGD) {Log.d(TAG, DBConstants.DB_NOT_EXIST);}
}
super.onCreate();
}
}
You should read this about the Android Acitvity Lifecycle: http://developer.android.com/training/basics/activity-lifecycle/index.html
In short: onCreate() will be called when the Activity has to be created.
If you launch an app, tha Activity will have to be created. If you go to background, the Activity will most probably not be destriyed until certain time passes, so when you return to the app there will be no need to create the Activity (it wasn't destroyed).
If you want your code to execute every time, you should user onResume(). onResume() is called every time before the Activity is showed.
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
I am trying to generate a notification from a class, Utilities.java, outside of the subclass of Context. I've thought about providing a SingletonContext class and have looked at posts ike this. I'd like to be able to return != null Context object since the notification can be generated at any given time because it is generated from a messageReceived() callback.
What are there downsides to doing something like this:
public static Context c;
public class MainActivity extends Activity{
#Override
public void onStart()
super.onStart()
c = this.getApplicationContext();
}
//other method somewhere outside this class
public Context getContext(){
return MainActivity.c
}
I don't think it would be any different than putting this on the onCreate(), however, it guarantees that the context is up to date when the activity starts.
The Context keeps a reference to this activity in memory, which you might not want. Perhaps use
this.getApplicationContext();
instead. This will still let you do file IO and most other things a context requires. Without a specific reference to this activity.
Maybe you should overwrite the onResume Method.
If you open a new activity, and switch back, the onStart method will not getting invoked.
Android Lifecycle: doc
BTW: I read about problems with ApplicationContext using a dialog or toast, so if you use the context to create on of these you should use your Activity as context.