Can we destroy activity without call stop() method? - android

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

Related

Removing Preference data when Application killed

In my application i am storing some SharedPreference data.
I have to clear that all stored data when application killed.
So, I have done it in my activity's onDestroy() as below :
#Override
protected void onDestroy() {
if(isBackPressed==0){
if(Prefrences.checkPref(MyActivity.this,MAIN_PREF)){
Prefrences.removePref(MyActivity.this,MAIN_PREF);
Prefrences.removePref(MyActivity.this,PREF_1);
Prefrences.removePref(MyActivity.this,PREF_2);
Constant.displayLogE(">>>>>>>>>>","### Prefrence removed ");
}
Constant.displayLogE(">>>>>>>>>>","### Destroy activity ");
}
finish();
super.onDestroy();
}
Here, I have taken isBackPressed because, When onBackPressed called it calles finish() automatically and onDestroy() method calls. So, I have initialized isBackPressed to 1 inside onBackPressed() method.
It doesn't matter, I just have to remove my prefrence data when app going to be killed. But, the issue is when I killing app, onDestroy() methos not calling.
Thanks.
try this way.
public class App extends Application{
#Override
public void onCreate() {
doSomeCleanWork();
}
}

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.

How to call an activity's onResume method from a Receiver

I have an activity named GameTray where drawings carried are out. I have a ScreenReceiver which extends BroadcastReceiver, this is for detecting screen lock and phone lock. I want to save the datas while a screen lock occurs in the GameTray activity. onResume method of GameTray contains all the saving things. So I have a doubt, How can I call the onResume method method of GameTray activity in the ScreenReceiver class while overriding onReceive method? Am attaching the source for ScreenReciever here. Thanks in advance
public class ScreenReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
// Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF");
// onPause() will be called.
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
// Log.v("$$$$$$", "In Method: ACTION_SCREEN_ON");
// HERE I WANT TO CALL THE ONRESUME METHOD OF THE ACTIVITY GAMETRAY.
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
// HERE I WANT TO CALL THE ONRESUME METHOD OF THE ACTIVITY GAMETRAY.
}
}
}
onResume method of GameTray contains all the saving things.
onResume() is a method that is part of an Activity life cycle, and it is called when the Activity comes to foreground, if you want to call it, you should start/resume the activity.
But why not create a separate class, or method, that will do all the saving things and be called in onResume() as well as in onReceive().

Android activity state

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.

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