1

Imaginarily, I have a parent activity called 'P'. Mr P has 5 children, called '1','2','3','4','5'.

But the truth in my project is ,

  • P have 1 as child.

  • 1 have 2 as child.

  • 2 have 3 as child.

  • and so on.

I travel from P to 1, and from 1 to 2, from 2 to 3, and so on.

The problem is how can i travel from 3 straight to P ? therefore P must be his parent am i wrong?

Here is what i have implemented so far on my back pressed

@Override 
    public void onBackPressed(){
      if (background_tick != null){
          if(background_tick.isPlaying())
              background_tick.stop();
          background_tick.release();
         // finish();
      }
      Intent crush=new Intent(getApplicationContext(),P.class);
      startActivity(crush);
      //super.onBackPressed();
 }

With that code i successfully go to activity P, but when i press 'Back' from P it return to acitivy 3 and i dont want that happen.

Any answer will appreciated guys. greetings from bali.

000
  • 26,951
  • 10
  • 71
  • 101
studentknight
  • 143
  • 1
  • 3
  • 9
  • 1
    Good Afternoon gentlemen. - depends where in the world we are. Maybe we are ladies. – Scary Wombat Mar 03 '14 at 08:59
  • simply call `finish()` after `startActivity(crush)` – SMR Mar 03 '14 at 08:59
  • possible duplicate of [How to Controll Android back stack](http://stackoverflow.com/questions/14142936/how-to-controll-android-back-stack) – Merlevede Mar 03 '14 at 09:03
  • Mr SMR : it didn't worked. The same problem happen. Userblabla : Sorry, then for you good evening ladies. – studentknight Mar 03 '14 at 09:03
  • Just so you understand why your code didn't work. Activities are not strictly parent or child. Its more like building a cake, u used a P layer, then a C layer, then a P layer again. So when you remove the last P, you are back to C. – NameSpace Mar 03 '14 at 09:05

2 Answers2

3

Set your Intent Flag . it remove activity from top.

Intent crush=new Intent(getApplicationContext(),P.class);
crush.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(crush);

Intent Flag

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
  • Maybe add a link to http://developer.android.com/guide/components/tasks-and-back-stack.html#ManagingTasks to explain the different flags and their behaviour. – rusmus Mar 03 '14 at 09:05
1

Uses the Flag

 Intent i = new Intent(3.this,P.class);
 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(i);
Piyush
  • 18,895
  • 5
  • 32
  • 63
Srikanth
  • 584
  • 1
  • 6
  • 21