Android: a scenario in which onPause is called but not onStop?

I am trying to understand the difference between onPause and onStop.

I read all the different forums, but I still do not quite understand this difference. I created a simple application to try and check when this method is called. To do this, I simply placed the loggers in each method.

From my trials -

  • Popups do not call a method
  • Switching to another activity calls both methods.
  • Disabling the notification bar does not call any method

I saw only that both methods are called in quick succession or not called at all. I am trying to find scripts where onPause is being called, but onStop does not.

The goal is to understand if an onPause definition is required. If scripts in which only onPause is called are so rare, there’s no point in writing separate code for onPause. Do I need to write onStop?

public class LifecycleActivity extends ActionBarActivity { @Override protected void onDestroy() { super.onDestroy(); Log.d("Rachit", "In Destroy Method"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lifecycle); Log.d("Rachit", "In Create Method"); } @Override protected void onStart() { super.onStart(); Log.d("Rachit", "In Start Method"); } @Override protected void onResume() { super.onResume(); Log.d("Rachit", "In Resume Method"); } @Override protected void onPause() { super.onPause(); Log.d("Rachit", "In Pause Method"); } @Override protected void onRestart() { super.onRestart(); Log.d("Rachit", "In Restart Method"); } @Override protected void onStop() { super.onStop(); Log.d("Rachit", "In Stop Method"); } } 
+11
source share
8 answers

I realized this later, but forgot to post my answer here.

The case when I noticed that onPause () was called without an immediate subsequent call to onStop () was when I received a notification from another application when another application was open on my phone.

For example, let's say that Facebook is currently running on your phone. If you receive a notification from WhatsApp (in a pop-up dialog box), your Facebook activity will be suspended. If you close the WhatsApp pop-up during this time, Facebook will resume activity. On the other hand, if you open WhatsApp from a pop-up message, your Facebook activity will be stopped.

+14
source

This script occurs when you start DialogActivity from the current activity, then onPause() current activity is called, and after going to the current Activity , onResume() called.

See Developer Docs for more information.

+9
source

Activity A β†’ Activity B If B is transparent, A onPause will be called, but not onStop.

+5
source

On the official Android developers page, you can see all the information related to these two activity states.

onPause () ( http://developer.android.com/training/basics/activity-lifecycle/pausing.html )

When the system calls onPause () for your activity, it technically means that your activity is still partially visible, but most often it indicates that the user is leaving this operation, and this will soon enter the Stop state. Usually you should use the onPause () callback in:

Stop animation or other ongoing activities that the processor may consume. Commit unsaved changes, but only if users expect these changes when they leave (for example, an email project). The release of system resources, such as broadcast receivers, knobs for sensors (such as GPS) or any resources that may affect battery life, while your activity is paused and the user does not need them.

As a rule, you should not use onPause () to store user changes (for example, personal information entered into the form) for permanent storage. the only time when you should save user changes to persistent storage within onPause () is when you are sure that users expect the changes to be automatically saved (for example, when composing email). However, you should avoid doing heavy CPU work during onPause (), for example, writing to databases, as it can slow down the visible transition to the next one (you should instead perform operations to disconnect a large load during onStop ()).

You should keep the number of operations performed by the onPause () method relatively simple in order to ensure a quick transition to the user if your activity is actually stopped.

Note. When your activity is paused, the Activity instance is resident in memory and called when activity resumes. You do not need to reinitialize the components that were created during any of the callback methods leading to the resumed state.

onStop () ( http://developer.android.com/training/basics/activity-lifecycle/stopping.html )

When your activity receives a call to the onStop () method, there is no longer visible and should release almost all resources that are not necessary until the user uses it. As soon as your activity is stopped, the system can destroy the instance if it needs to restore the Memory system. In extreme cases, the system can simply kill your application process without causing the completion of the action in the onDestroy () callback, so it is important to use onStop () to release resources that may leak memory.

Although the onPause () method is called before onStop (), you should use onStop () to do more intensive shutdown of the processor operations, such as writing information to the database.

When your activity is stopped, the Activity object is stored in memory and called when activity resumes. You do not need to reinitialize the components that were created during any callback to the methods leading to the Resumed state. The system also keeps track of the current state for each view in the layout, so if the user entered the text in the EditText widget, this content is saved so you do not need to save and restore it.

Note. Even if the system destroys your activity when it stops, it still saves the state of View objects (for example, text in EditText) in the Bundle (blob key-value pairs) and restores them if the user goes to the same instance of activity (next In the lesson more is said about using the Bundle to save other state data if your activity is destroyed and recreated).

+2
source

The result of my test:

For the two actions below, when starting C4_SecondActivity from MainActivity , for MainActivity , its onPause but not onStop .

 <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".C4_SecondActivity" android:label="@string/title_activity_c4__second" android:theme="@style/Theme.AppCompat.Dialog"> </activity> 
+1
source

When we start the dialog operation from the current Activity, only onPause() will be called at this time. Example: suppose we have activities A and B. Currently, activity B is activity and activity A is normal activity. when we start activity B from activity A, then activity A will be called onPause() , and actions B onCreate() , onStart() and onResume() will be shown. Please check below url for sample code sample code

0
source

There is another way to do this. let's take a script in which you start an activity mode and create a notification from it and start it and call the same pending intention again, after which onresume will be called post to on pause.

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotification(); } private final static String TAG = "TestOne"; public void sendNotification(){ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); //Create the intent that'll fire when the user taps the notification// Intent i = new Intent(MainActivity.this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0); mBuilder.setContentIntent(pendingIntent); mBuilder.setSmallIcon(R.mipmap.ic_launcher); mBuilder.setContentTitle("My notification"); mBuilder.setContentText("Hello World!"); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(001, mBuilder.build()); } 

Manifesto:

  <activity android:name=".MainActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Main2Activity"></activity> 
0
source

onPause() activity method is a call when you receive a phone call. Otherwise, in many cases, onPause() always called with onStop() . For example, when you press the home button, call up another intention and more, for example, when your activity is in the background.

-1
source

Source: https://habr.com/ru/post/990034/


All Articles