Naseeb Sheoran
Naseeb Sheoran

Reputation: 483

Using OnPause and OnResume in an application

I am making an application in which i want to let the control go to some activity and then to come back. When I have searched about that problem, I came to know that this problem could be solved by using OnPause() and OnResume(), but I don't know how to use them.

This is the code where I am transferring the control to some other activity. Please tell me how to use OnPause() and OnResume() in this regard.Thanks in advance.

Intent intent = new Intent(MCQ.this,ConfigActivity.class);
            int ClassIndex = 2;
            intent.putExtra("ClassIndex", ClassIndex);
            startActivity(intent);

Upvotes: 0

Views: 1572

Answers (3)

Adam Monos
Adam Monos

Reputation: 4307

You are not making too much sense. When you call startActivity(), Android will put your current Activity to the background, and start (or bring to foreground) the new Activity specified in your Intent.

onPause() called when your Activity disappears from the screen, eg. the system either puts it in the background, or terminates it.

onResume() is called when your Activity becomes visible on the screen, eg. either after onCreate() if Android created a new instance of your Activity, or when the system brings your Activity back from the background.

There is no explicit passing of control between Activities. If it is on the top of the activity stack (visible on the device screen), than you can interact with it.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Upvotes: 3

DeeV
DeeV

Reputation: 36045

I think you may be confused as to what onPause() and onResume() are used for.

onResume() is called when the Activity is about to show it's view to the user. You can use your intent in this method so the main Activity would instantly go to the ClassIndex activity. However, when you leave the ClassIndex activity, onResume() will be called again which will send you back to ClassIndex.

onPause() is called when the Activity is leaving. Whether this is for locking the screen or moving to another Activity. In this case, when you call startActivity in the main Activity, Android will call onPause() in the main activity before entering the new one.

What you may want in this case is to use startActivityForResult() and use it to determine if you're coming back from your ClassIndex activity or not. If not, then start it. If so, then move on to the main activity.

A better solution may-or-may-not be to start the app in ClassIndex and move to your main activity when the user is done with it. This would be the case if you want to move to ClassIndex every time the user enters the app.

Upvotes: 1

Martze
Martze

Reputation: 921

onPause() and onResume() are called by android.
Your job is to implement these methods. onPause and onStop are called at your launching activity by Android, and onCreate, onStart and onResume at the Activity your starting. If you want to go back, you usually just press the back button at your device.
You might want to take a look at the Android devGuide and especially at the Activity lifecycle.

Upvotes: 1

Related Questions