1

This is my part of a code.when i click the button"play" i need to take it to the previous activity which is the main activity.Have tried this code but this is not working.Iam new to android ..Plz help.Thanks in advance!!

                    Button play;
                    case R.id.playid:
        Intent i = getIntent();
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
              @Override
      public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    startActivity(new Intent(this, MainActivity.class));
}
anusha
  • 55
  • 2
  • 10
  • possible duplicate of [Sending data back to the Main Activity in android](http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android) – Sudhir Mishra Apr 06 '14 at 06:05

2 Answers2

1

Take a look at the launch modes for activity inside manifest. Unless specified a custom behavior, your activities called one after the other will form a stack structure.
This will help: http://developer.android.com/guide/topics/manifest/activity-element.html

In order to return to the previous activity, you just need to finish the current one then, which is done by finish(); or context.finish(); where context can be "this" or declared exclusively.
Recommend you to read this page, the whole:
http://developer.android.com/reference/android/app/Activity.html#finish%28%29

Also, for onBackPressed, in your case, you may not override the default implementation:

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

Source: http://developer.android.com/reference/android/app/Activity.html#onBackPressed()

Pararth
  • 8,114
  • 4
  • 34
  • 51
1

You need to use startActivityForResult ,in this process you start activity B From A and when B finished b clear from top of android stack

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

and in SecondActivity

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);     
finish();

Update: i forgot main point. and in first Activity

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (requestCode == 1) {

 if(resultCode == RESULT_OK){      
     String result=data.getStringExtra("result");          
 }
 if (resultCode == RESULT_CANCELED) {    
     //Write your code if there's no result
     } 
   }
 }

and u also can send another result in Second Activity such bellow :

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();
Amin Bahiraee
  • 538
  • 10
  • 20
  • There are two activities a and b.After b is completed i need to go back to "a" when button is clicked.@Bigoloo – anusha Apr 06 '14 at 04:35
  • Where the first two lines should be added?Either in first or second activity @bigoloo – anusha Apr 06 '14 at 04:36
  • hi first two line should add to where u want to start Second Activity @anusha startActivity and startActivityForResult is same but in startActivityForResult in second Activity u create an intent and set resuklt on it and finish it and i forgot most important part – Amin Bahiraee Apr 06 '14 at 04:51