0

I want to stop an activity from finishing . I know I have to override onBackPressed and remover super.onBackpressed. and do my coding. know i want to know what should i write in onBackpressed to not to delete it but put it in background.I found a solution below.

  @Override
      public void onBackPressed() {
        moveTaskToBack(true);
        return;
      }

but it will close the application and act like a home button. But i want to go to the previous Activity where i came from. And one major thing i need is .. I have a button to open this particular activity from any where it should be still where i left of..because it is an AudiopLAYER WITH a seekbar..from where ever i call it .. it should show its current progress. I mean i need a way to hide an activity on Background. and from any Activity i call it . it should call from the stack. i don't want to open a new instance of it.. Through googling i got an idea but don't know how to implement it..

@Override public void onBackPressed() {

    //super.onBackPressed();
    Intent backIntent = new Intent(this, SongsList.class);
    backIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(backIntent);
}

A question related to this is here.

I wanted to do this but from any other activity it should be shown..

I find a solution to backPressed but how could i change it to my needs..

  @Override
    public void onBackPressed() {
       Log.d("CDA", "onBackPressed Called");
       Intent setIntent = new Intent(Intent.ACTION_MAIN);
       setIntent.addCategory(Intent.CATEGORY_HOME);
       setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(setIntent);
    }
Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

2 Answers2

1
@Override
    public void onBackPressed() {    
        Intent backIntent = new Intent(this, AudioPlayer.class);
        startActivity(backIntent);
    }

Try this.. this will call your new activity and it doesnot close the current one.

But it will stored in a stack. If you need not want the history put

noHistory = true

in your android manifest

Ciril
  • 420
  • 3
  • 9
  • from AudioPlayer i move to SongsList then it will be in background. But how could i call AudioPlayer Activity from other Activities . Because it is associated with seekbar and some other attributes. – Zar E Ahmer May 20 '14 at 10:54
  • I mean the above code put it in stack. then how could i call an activity from stack. may be it is beneath some other activity. – Zar E Ahmer May 20 '14 at 10:55
  • Assumes you are in AudioPlayer activity.. and you open songsList activity.. if you want to move back just press the back button without overriding it. it will backup the stack. Am I got it correct or your need is different ? – Ciril May 20 '14 at 11:05
  • how could i call the activity which is in stack . May be it is below some other activity?.. Thanks it helped but not completely solve my problem.. – Zar E Ahmer May 20 '14 at 11:47
  • Check the code I just edited. If you are at SongsList activity then code the above to move on AudioPLayer class – Ciril May 20 '14 at 13:33
  • Your code help a lot i have found the solution to my problem here.. http://stackoverflow.com/questions/5914040/onbackpressed-to-hide-not-destroy-activity/23759328#23759328 – Zar E Ahmer May 20 '14 at 13:36
0

Ciril answer is correct to some extent. Because by overridding onBackPressed we came back to previous activity without finishing it. But it is not final , if android need resources , it can delete it. I don't find a way to avoid deleting it. but my app is working fine . And main thing is to call the Activity which may be down in the stack and opening that particular activity is must.

The code to open an activity from stack and reorder it to top of stack is..

Intent intent = new Intent(Album.this, StaticAudioPlayer.class);

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);// this flag reorder the activity to top 
startActivity(intent);
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300