how to move activity to background in android without pausing it? - android

I want ask like in title. how to move/send activity to background in android without pause it?
for example in this method:
public void onBackPressed () {
//here what i should wrote to back my previous activity and not stop or pause current
}

That is not possible. By definition:
onPause() will be called when the activity no longer is in the foreground from an input standpoint
onStop() will be called when the activity no longer is in the foreground from a visibility standpoint

Related

Android. Automatically closing second activity in a minute after app goes to background

Suppose my android app has two activities A and B. Activity A starts activity B (A->B). When activity B is shown a user presses "Home" button and the application goes to background. Now I want activity B to be closed automatically in background (because it has some secret information displayed), say, in a minute after the user pressed "Home" button. Which is the best way to implement this?
I tried to do it using Handler and postDelayed() placed in onStop() of activity B, but it seems like it sometimes doesn't work and activity B stays not finished when the app is resumed from backround...
My code is:
#Override
protected void onStop() {
super.onStop();
mLockHandler = new Handler();
mLockRunnable = new Runnable() {
#Override
public void run() {
finish();
}
};
mLockHandler.postDelayed(mLockRunnable, 60 * 1000);
}
Android is able to finish activity by itself when it doesn't have enough resources.
If that happened, I suppose the handler is not available as well. If you very want to handle your activity finishing, you need to create some class, which will survive during activity lifecycle(AKA Singleton). That wouldn't let android to remove it from memory.
Although you would have another couple problems:
Your singleton won't be able to finish activity if it is in a wrong state.
Putting activity to singleton will lead to memory leak.
Summarising, trying to change activity lifecycle is a bad idea. Maybe some Service class will solve your task in background?

Close Transparent Activity by calling onDestroy/ Lifecycle of Transparent Activity

I have simple code with transparent activity (it is imposed on my MainActivity). I need to kill this transparent activity by clicking button and after that I need to show dialog. But get some problems first of all if I do that :
public void buttonClick(View view) {
if (view.getId() == R.id.bToK)
{
onDestroy();
}
}
transparent activity it's still visible just nothing happen (in debug mode I see that it goes to onDestroy but I doesn't destroy it at all)
If I change onDestron() to finish() there are other problems because my Main activity became first visible (onResume is called) then called is onDestroy for transparent what provides to next problems during creating this dialog. What should I do If i want show this dialog after kill this transparent ?
Call finish() instead. This will call onDestroy() and respect the activity life. Next if you want open a dialog you can start it before finish or start in the main activity with onResult https://developer.android.com/training/basics/intents/result.html

Destroy / Pause Android Service (music) on HomeButton pressed

I tried to add some Music in the Background of my Main Menu. I finally achieved it by using extends Service. Now i have another problem:
If the Home Button is pressed, the Service still runs and plays music, it only finishes, if i destroy it. I tried using:
#Override
protected void onPause(){
super.onPause();
service.setAction("model.BackgroundSound");
stopService(service);
}
which works fine, but if i switch from my Main Menu to my Character Selection Activity, it will pause too. But i would like to keep the music going in all Activities.
Is there a way to detect the difference of the App being "on pause" or just the activity?
You can use fragments instead of Activities and it will call your Pause() just for the Single Activity.
If you are using API level 14 and higher you can use registerActivityLifecycleCallbacks() on the Application. You can check and example usage in this SO answer.
This way you can check if any of your app's Activities is visible.

Calling a method in android during onload event

onCreate() does not get called when I leave the application using back button and start it immediately. I believe this is because, Android has not killed the application process yet. I tried using #AfterViews, the same happens. How could I make sure that every time the application is started, my specific code runs?
Does not get called when I leave the application using back button and then start.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodToRun();
}
I use this onBackPressed() to exit the application.
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
this.finish();
}
Even #AfterViews does not get called when I leave the application using back button and then start.
#AfterViews
void checkAgreementFlag(){
methodToRun();
}
I want methodToRun() to always get called. How would I do it?
Call methodToRun() from onResume() of activity.
onResume(): Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
onCreate(): is called when your application is starting first time.
onRestart(): Called after your activity has been stopped, prior to it being started again.
onStart(): Called when the activity is becoming visible to the user.
onStop() : When the activity is no longer visible.
For the back key scenario activity lifecycle:
onCreate()->onStart()->onResume()->Activity running
After pressing back key
onPause()->onStop()
If the activity is coming back then
onRestart()->onStart()->onResume()->Activity running
otherwise onDestroy() will be called.

when touch back button on android how to quit the app by cocos2d-x

A game created by cocos2dx. In active scene, when I touch the back button on android, how can i quit it!
can give same example?
Version cocos2d-2.0-x-2.0.4 and cocos2d-x-2.1.4
in our Layer.h:
...
void keyBackClicked();
...
in our Layer.ccp:
Layer::init(){
...
this->setKeypadEnabled(true);
...
}
void Layer::keyBackClicked() {
CCDirector::sharedDirector()->end();
}
Now you are in a position if you click back then you are navigated to the previous screen where you started off your game application right? Then here is the solution: after navigating to the new intent, the previous screen gets in an inactive state in android activity lifecycle you can find that the previous screen goes to a invisible state. Now we can use onPause() method to close the hidden activity. In the same class add this code and your app should be closed when you press back button.
protected void onPause() {
super.onPause();
finish();
}
When you click any button and go to a new intent the application goes to the invisible state and the onPause() method is triggered automatically and it closes the same intent in background.
Override backKeyClicked() method in your layer.
Don't forget to add this->isKeypadEnabled(true) in init method of layer.
In you backKeyClicked method, you can switch it to previous scene or whatever you wanna do.

Categories

Resources