Android activity lifecycle confused between calls of function - android

When I run this app I am getting "On Resume" first instead of "On start" even "On create" not showing up, please tell me why? and "On Restart" toast is not displaying but test is getting updated.
public class MainActivity extends AppCompatActivity {
public int test=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast t=Toast.makeText(getApplicationContext(),"On create",Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER_VERTICAL,20,20);
t.show();
}
#Override
protected void onRestart() {
super.onRestart();
Toast t=Toast.makeText(getApplicationContext(),"On restart",Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_VERTICAL,20,20);
TextView num=(TextView) findViewById(R.id.textNum);
test++;
num.setText(String.valueOf(test));
t.show();
}
#Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"On start",Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
Toast t=Toast.makeText(getApplicationContext(),"On resume",Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER_VERTICAL,10,20);
t.show();
}
#Override
protected void onPause() {
super.onPause();
Toast t= Toast.makeText(getApplicationContext(),"On Pause",Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER_VERTICAL,20,20);
t.show();
}
#Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"On Stop",Toast.LENGTH_SHORT).show();
}
#Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"On destroy",Toast.LENGTH_SHORT).show();
}
}

Using Toast as a debugging method is really bad idea. Use logging and look at your logcat. You will see that the methods get called in the order that they should. Just don't do this.

Related

OnResume is firing instead of OnStart and onCreate

I want to show toast on every life cycle but upon running the application the OnStart and OnCreate gets skipped and OnResume only runs, same with other life cycle methods. But whenever II remove the OnResume, OnStart is firing but onCreate is not.
EDIT:
this code works, but I run the application on my smartphone which is Xiaomi.
Tried running on emulator and everything works, correct me if I'm wrong
my code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//launch
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "Created!", Toast.LENGTH_LONG).show();
}
#Override
public void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "Started!", Toast.LENGTH_LONG).show();
}
#Override
public void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "Resume!", Toast.LENGTH_LONG).show();
}
//stopping
#Override
public void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(), "Pause!", Toast.LENGTH_LONG).show();
}
#Override
public void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(), "Stop!", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "Destroy!", Toast.LENGTH_LONG).show();
}
#Override
public void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(), "Restart!", Toast.LENGTH_LONG).show();
}
}
EDIT: this code works, but I run the application on my smartphone which is Xiaomi. Tried running on emulator and everything works, correct me if I'm wrong

how to check if android going to recreate activity or destroy?

I have an android studio project. When I am rotating screen, android destroys and recreates main activity. How can I check during the destruction, if android going to recreate activity?
You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy.
#Override
protected void onDestroy() {
super.onDestroy();
if (isFinishing()) {
// wrap stuff up
} else {
//It's an orientation change.
}
}
Another alternative (if you're only targeting API>=11) is isChangingConfigurations.
#Override
protected void onDestroy() {
super.onDestroy();
if (isChangingConfigurations()) {
//It's an orientation change.
}
}
Override the Activity lifecycle methods to see the flow.And then use the appropriate method to check activity current state like isChangingConfigurations()
Example code snippet.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
Log.i(MainActivity.class.getSimpleName(),"OnStart Called");
}
#Override
protected void onRestart() {
super.onRestart();
Log.i(MainActivity.class.getSimpleName(),"OnRestart Called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.i(MainActivity.class.getSimpleName(),"OnDestroy Called");
}
#Override
protected void onPause() {
super.onPause();
Log.i(MainActivity.class.getSimpleName(),"OnPause Called");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(MainActivity.class.getSimpleName(),"OnConfiguration Changed Called");
}
}
For more details see the official page activity-lifecycle

SwitchPreference.isEnabled() not detecting if enabled/disabled properly

I'm trying to use SwitchPreference and trying to detect it's state using isEnabled() method.
Here's the code (in SettingsActivity.java):
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
spChanged = new
SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// your stuff here
if (key.equals(KEY_ENABLE_F)) {
SwitchPreference fPref = (SwitchPreference) findPreference(key);
if (fPref.isEnabled()) {
Toast.makeText(getBaseContext(), "Enabled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Disabled", Toast.LENGTH_SHORT).show();
}
}
}
};
}
#Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(spChanged);
}
#Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(spChanged);
}
The problem is that the Toast with text "Enabled" is showing up no matter if switch in 'ON' or 'OFF'.
What could be wrong here?
Whoops! I should have tried it before posting the question. Anyways, I solved the issue by changing:
fPref.isEnabled()
to this:
fPref.isChecked()

Stop the toast while quitting the app

I was doing an application which uses some Toasts.
If a Toast appears and in the meanwhile I quit the app, it normally doesen't disappear.
Is there a way to stop the Toast if I quit the app on my phone?
In Activity onStop or ondestroy use cancel() method
public class MainActivity extends Activity {
private Toast toast = null;
#SuppressLint("ShowToast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG);
showMyToast();
}
public void showMyToast()
toast.setText(" test toast");
toast.show();
}
#Override
protected void onStop () {
super.onStop();
toast.cancel();
}
while quitting from add need to write this
toast.cancel();
Toast.makeText returns a Toast object. Call cancel() on this object to cancel it.
Toast toast = Toast.makeText(this, "Hello..", Toast.LENGTH_LONG);
toast.cancel();
use android activity life-cycle and override onstop() or onPause mthodes
onPause() called when activity is not visible to the user.
onStop() called when activity is no longer visible to the user.
#Override
protected void onPause () {
super.onPause();
toast.cancel();
}
or
#Override
protected void onStop () {
super.onStop();
toast.cancel();
}

Kill single activity Android app

I'm an Android newbie. I'm writing an app to trace the Activity lifecycle using Log statements. I want to kill my app in order to see the onDestroy() event being called. I've added a button that calls finish to do this, but I've not been able to terminate the app. I've also tried System.exit(0), but my app won't terminate. What am I doing wrong?
public class MainActivity extends Activity {
private static final String LOG_DISPLAY = "DEBUG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_DISPLAY, "onCreate called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_DISPLAY, "onPause called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(LOG_DISPLAY, "onResume called");
}
#Override
protected void onStop() {
super.onStop();
Log.d(LOG_DISPLAY, "onStop called");
}
#Override
protected void onStart() {
super.onStop();
Log.d(LOG_DISPLAY, "onStart called");
}
#Override
protected void onRestart() {
super.onStop();
Log.d(LOG_DISPLAY, "onRestart called");
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
//System.exit(0);
}
});
}
}
You must override onDestroy in order to check if it is called, and call your addListenerOnButton method too:
Try with this:
public class MainActivity extends Activity {
private static final String LOG_DISPLAY = "DEBUG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_DISPLAY, "onCreate called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_DISPLAY, "onDestroy called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_DISPLAY, "onPause called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(LOG_DISPLAY, "onResume called");
}
#Override
protected void onStop() {
super.onStop();
Log.d(LOG_DISPLAY, "onStop called");
}
#Override
protected void onStart() {
super.onStop();
Log.d(LOG_DISPLAY, "onStart called");
}
#Override
protected void onRestart() {
super.onStop();
Log.d(LOG_DISPLAY, "onRestart called");
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
MainActivity.this.finish();
//System.exit(0);
}
});
}
}
First add missing onDestroy implementation to show you logs :
#Override
protected void onDestroy() {
Log.d(LOG_DISPLAY, "onDestroy called");
super.onDestroy();
}
Then simply open activity and exit it with hardware back button. You will see onDestroy logs.
You will have same effect calling finish() programmatically as well, just dont forget to place call to your addListenerOnButton somewhere within onCreate

Categories

Resources