0

Suppose there are two activities A(Main Activity) and B . if we press a button on Activity A , Activity B starts , and if we press the back button on activity B , Activity A comes back . And if we then press Back button on Activity A screen , we comes out of the app .

I want to ask is there a way to keep the Main Activity Active . Like if we again open the app the on create method should not be called . And we return to the same state of the Main Activity as before pressing the back button.

For example in a Music Player app , when we press the back button from the app the app closes but the music still keeps playing and when we open the app again the music does not starts over again. means the onCreate method is not called again

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ronit Jain
  • 45
  • 2
  • 9

5 Answers5

2

Try to override onBackPressed()

@Override
public void onBackPressed() {
     //here you can call super.onBackPressed() with some condition
}

Duplicate how to disable back button in android

onBackPressed() actually calls the finish internally, so by overriding it you are indirectly disabling the finish()

If you want to show another screen then

@Override
public void onBackPressed() {
     Intent itent = new Intent(this, MyHomeScreenActivity.class);
     startActivity(itent);
}

If you want to move the app to background then you need to override onKeyDown() refer this

Moving application in background on back button event

Community
  • 1
  • 1
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
  • i don't want to disable the back button . i want not to call the finish function when back button is pressed. Like when the app is running we open the task manager of the phone and we can see that our app is still in the active applications. The same i want when the back button is pressed in my app. – Ronit Jain Jun 27 '15 at 07:53
  • okay i got it now .But the problem is that the home screen is still not called . I placed onBackPressed(){//empty} method but the app does not call the home screen . – Ronit Jain Jun 27 '15 at 08:02
1

When u want to pressed back button then u must implement onBackPressed

 @Override
 public void onBackPressed() {
 //here you can call super.onBackPressed() with your require code
}
jeet parmar
  • 868
  • 8
  • 19
0

I would recomand to read the Android activity API and description. What you want to do is simply launch an activity over an other without making a call to the finish function.

In order to do so just overide the onBackPressed function as follow

@Override
public void onBackPressed()
{
     Intent intent = new Intent(this, BActivity.class);
     startActivity(intent);
     super.onBackPressed();  // optional depending on your needs
}
hkN
  • 135
  • 4
  • i want to launch the home screen from main activity on back button pressed , keeping the main activity still active , means not calling the finish function of the main activity – Ronit Jain Jun 27 '15 at 07:49
0

I just looked at your comments and based on your requirement mentioned there, that app should launch the home screen from main activity on back button pressed , keeping the main activity still active, You can write below code:

public void onBackPressed() {
     Intent mainActivity = new Intent(Intent.ACTION_MAIN);
     mainActivity.addCategory(Intent.CATEGORY_HOME);
     mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(mainActivity);
 }

This code will launch the home screen from main activity or Activity A in your question on back button pressed , keeping the main activity still active

AADProgramming
  • 6,077
  • 11
  • 38
  • 58
  • But when we remove all the recent apps(pressing the home button from the phone for 2 seconds and then remove all recent apps ) My app in no longer active . i want my app to remain active even after removing all the recent apps – Ronit Jain Jun 27 '15 at 08:25
  • understand that it is difficult to cover all possible doubts that you have related to different use cases however, for your recent query, as you use remove all option, it is as good as hitting back button to destroy activity. Hence, Android is correctly behaving. WHY you want to prevent user from removing YOUR App ? check this link for more details http://lifehacker.com/what-happens-when-you-remove-an-app-from-androids-mult-1179868228 – AADProgramming Jun 27 '15 at 09:13
0

in a Music Player app , they use Services http://developer.android.com/guide/components/services.html

You should use it for applictaions run in the background

I recommended you for go through this tutorial MusicDroid - Audio Player Part.

There are three parts of these tutorial. Its nicely describe for how to implement Audio player for android using service and AIDL.

Also look at this android developer tutorial Media Playback.

Maher Ismaail
  • 1,663
  • 2
  • 15
  • 26