Android activity state - android

In Android applications, is it possible to be notified when the activity is paused
but without using onDestroy method in the activity class.
In other words, I want the operations to be outside the main class.

You can use onPause() method of Activity life cycle:

just override onPause method.
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
When activity is going to pause. it will call this method.

Related

Resume Activity from Fragment

I tried several times to invoke Resume method of MainActivity from the Fragment,
calling simply onResume(); nothing in result.
code for resume method
protected void onResume() {
all.clear();
outgoing.clear();
incoming.clear();
getFromSdcard();
super.onResume();
}
I want to call this method from fragment.
Enhancing #cricket_007 response :
You can invoke onResume from the parent Activity, but just like you have seen, onResume has protected access blocking you from calling it.
There is a small visibility workaround that allows you to do it.
1 - Implement this method on your activity
public void myOnResume(){
this.onResume();
}
2 - Then on your fragment you can invoke
public void myFragmentMethod(){
// make sure to double check casts (to YourActivity) like these before invoking
((YourActivity) getActivity()).myOnResume();
}
Conclusion and recommendation : Even though it is not recommended that you implement it this way, it can be done like I said.
In my opinion, what you should do is :
1 - Have all the functionalities of your onResume() method inside a proper method of your own (you name it!) like :
public void clearData() {
all.clear();
outgoing.clear();
incoming.clear();
getFromSdcard();
}
2 - Then you could just separate all these functionalities from onResume() and still have them invoked like
protected void onResume() {
super.onResume();
clearData();
}
3 - Invoke it on your fragment like (previously shown)
public void myFragmentMethod(){
// make sure to double check casts (to YourActivity) like these before invoking
((YourActivity) getActivity()).clearData();
}
Let me know how it went.
Regards,
You are calling the Fragments onResume()
You can try getActivity().onResume(), but you really should make a method to do whatever code you need rather than explicitly call lifecycle methods.
Create an Interface
interface OnParentActivityResumed {
fun onActivityResumed()
}
implement the interface in the fragment
class MyFragment: Fragment(), OnParentActivityResumed{
override fun onActivityResumed() {
//[YOUR CODE WHEN PARENT ACTIVITY RESUMED]
}
}
on parent activity onResume add
override fun onResume() {
super.onResume()
myFragment.onActivityResumed()
}

Can we destroy activity without call stop() method?

In activity life cycle to execute as like.
onCreate()
onDestroy()
it means without call stop() method in activity life cycle.
how it is possible ?
Use finish(); to destroy activity.
You don't need to call stop() method. Android system automatically go thru those life cycle methods.
But apparently onDestroy() always called after onStop().
If you want to kill activity just call finish(), it will destroy your activity.
But remember again onStop() always called as system level, follows the activity life cycle if you call finish().
Note: If system kills your application or activity to utilize memory there is no guarantee to call these methods from activity life cycle.
public class ExampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty);
System.out.println("in onCreate");
finish();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("in onDestroy");
}
}
when the activity run then call onCreate() method and onDestroy()
method

When an activity with "singleTask" launchMode comes to Foreground - Android

I have an activity with "singleTask" mode. when this activity goes to background by taping on home key or other application, which methods will be called if it backs to foreground?
onCreate(Bundle savedInstanceState) with savedInstanceState != null
onRestoreInstanceState(Bundle savedInstanceState)
I think because there is one instance of this activity, it doesn't need to save and restore its state and it retain its own state always. am I right?
The sequence of methods that will be invoked is as follows,(in case when Android has killed the process hosting the Activity while the application was in the background, ) when Activity with singleTask mode comes in foreground:
1.onCreate 2.onStart 3.onRestoreInstanceState and 4.onResume
Below is sample code to demonstrate the concept: Activity Declaration in AndroidManifest.xml: <activity android:name="Second" android:launchMode="singleTask"></activity>
public class Second extends Activity {
EditText mEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview_not);
if (savedInstanceState!=null){
Log.e("onCreate of Actiity", savedInstanceState.getString("editval")); }
mEdit=(EditText) findViewById(R.id.editText1);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("Second", "onResume");
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("Second", "onStart");
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("editval", mEdit.getText().toString());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
Log.e("Second", "onRestoreInstanceState");
if (savedInstanceState!=null){
Log.e("onRestoreInstanceState", savedInstanceState.getString("editval"));
}
}
}
For illustration purpose, I am just using Edit Text and Android saves its state during configuration change, or when user press HOME etc.
Note that onSaveInstanceState(Bundle outState) is called before an activity may be killed so that when it comes back some time in the future it can restore its state using onRestoreInstanceState(Bundle savedInstanceState).
Also as mentioned by David in another Answer,If Android has not killed the process hosting the Activity, then bringing the task from the background to the foreground will not cause either onCreate() nor onRestoreInstanceState() to be called.
If Android has not killed the process hosting the Activity, then bringing the task from the background to the foreground will not cause either onCreate() nor onRestoreInstanceState() to be called. The Activity doesn't need to be created (it already exists) and the state doesn't need to be restored, because it hasn't been changed.
If, however, Android has killed the process hosting the Activity while the application was in the background, when the user returns to the application Android will create a new process for the application, create a new instance of the Activity, call onCreate() passing the saved instance Bundle as a parameter. It will also call onRestoreInstanceState() passing the saved instance Bundle as a parameter.

Deleting values when the user closes the application

So in my application I am using SharedPreferences to save fragment state. But I would like to delete those entries inside the SharedPreferences once the user steps outside of the application. I tried the following:
In my main class:
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(isFinishing() == true)
{
SM.removePreferences();
}
}
where SM is an instance of a helper class I created. removerPreferences does the following:
public void removePreferences(){
editor.clear();
editor.commit();
}
But I noticed that this was never executed. With the log, I did see that the app goes inside the isFinishing() if statement, but the method is never executed. I also did try the onDestroy(), but the method never got called.
Can someone help me on this ?
use onstop override to do that
like this:
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(isFinishing() == true)
{
SM.removePreferences();
}
}
Don't save your Fragment's instance state in SharedPreferences, but in the Bundle that is meant to do that. You can access it like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) {
// get your values, for example:
mID = savedInstanceState.getInt("ID");
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// put your values, for example:
outState.putInt("ID", mID);
}
This way you don't have to manage the values yourself.
If you have custom Objects, you can make them implement Parcelable.
In contrast, SharedPreferences are meant to save values that should persist even after the application closes, i.e.: preferences.
The better location to do that is onDestroy() without the if statement.
however, you can read from Android documentation about onDestroy() that:
There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
So, in my opinion, if you don't want data remains after activity ends, you should use a class to hold it.
I think that what you're looking for can be achieved using onSaveInstanceState mechanism.
Nonetheless, I'll try answering your specific question while assuming that by saying that "the method is never implemented" you mean that the if statement value is always false and that your method doesn't get called.
isFinishing() returns true only when you called finish() on the Activity or if someone else has requested that it will be finished.
If you just click on the home button, you will get isFinishing() == false, thus your method doesn't gets called. So make sure you're actually finishing the Activity and not just pausing it.
Anyways, the best way to find out what's the problem is to use the debugger.

Can i put flurry code in onCreate() and onDestroy()?

I am trying to use flurry for my android app. It says that i should put flurry code in onStart() and onStop() methods. I dont have these methods in my code. I have two activities and both use onCreate() and onDestroy() methods only. Can i put flurry code in that? Will there be any problem with it?
onStart() and onStop() are methods that handle part of an activity lifecycle, so you can add them to your activities without any problem.
#Override
protected void onCreate(...) {
super.onCreate(...);
...
}
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(this, "your_key");
}
#Override
public void onStop()
{
super.onStop();
FlurryAgent.onEndSession(this);
}
onStart and onStop are existing methods on an Activity, just like onCreate. If you want to add functionality at these points of the activity lifecycle, you can override them just like you did for onCreate.
public class MyActivity extends Activity {
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(this, "FLURRYKEY");
}
#Override
public void onStop()
{
FlurryAgent.onEndSession(this);
super.onStop();
}
}
onCreate and onDestroy are not an appropriate pair of methods to use for Flurry session tracking because onDestroy is not guaranteed to be called. See the documention on onDestroy. You can end up with situations where the app gets killed by the system and Flurry will think the session is still going.
There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Categories

Resources