0

I have three activites EmpSearch, SearchResults and EmpDetails.

When i click on the search button in EmpSearch activity, I am sending the input values of EmpSearch activity to SearchResults activity using intent. In SearchResults activity, I am passing the received input values to backend(OData services) and displaying the result in Listview. When I click on Listview item, I will navigate to EmpDetails activity. When I click on the emulator back button in EmpDetails activity, I am able to see the List filled with data previously fetched in SearchResults activity. Upto now everything is working fine.

I am using swipe gesture to come to SearchResults activity from the Details activity as shown below:

@Override
public void onSwipe(int direction) {

    String str = "";

      switch (direction) {

      case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";
                                                newPages();
                                               break;
      case SimpleGestureFilter.SWIPE_LEFT :  str = "Swipe Left";
                                                     break;
      case SimpleGestureFilter.SWIPE_DOWN :  str = "Swipe Down";
                                                     break;
      case SimpleGestureFilter.SWIPE_UP :    str = "Swipe Up";
                                                     break;

      }

}

    private void newPages() {

    Intent swipeRight = new Intent(this, SearchResults.class);
    swipeRight.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(swipeRight);
    finish();   

}

Now, when I navigate back to SearchResults activity by swiping to the right side in EmpDetails activity, it is showing empty list in SearchResults activity. How can I get the same behavior as emulator back button.

Please help me.

user2740599
  • 449
  • 4
  • 10
  • 20

3 Answers3

0

I think your problem may be solved if you call onBackPressed() in newPages() method of in Details activity

private void newPages() {

    onBackPressed();

}
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
  • I have one more doubt. On My application home screen, when I press on back button, I should an alert like "Do you want to exit?". How can I achieve this. – user2740599 Oct 21 '13 at 10:25
  • Hi Mukesh, By calling the finish() method in my newPages() method also, its working fine for me. My question is which one is good practice. Please clear my doubt. – user2740599 Oct 21 '13 at 11:08
  • you can use any of both because both use lastly `finish` of Activity. To create a dialog on home screen override `public void onBackPressed()` in that activity and create a dialog as at this link suggested.http://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android – Mukesh Kumar Singh Oct 21 '13 at 11:36
  • you may also create a simple Alert dialog as here http://www.mkyong.com/android/android-alert-dialog-example/ – Mukesh Kumar Singh Oct 21 '13 at 11:40
  • @user2740599 look here regarding `backPressed()` http://developer.android.com/reference/android/app/Activity.html#onBackPressed() – Mukesh Kumar Singh Oct 21 '13 at 11:46
0

Override your click listener for back button like this

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
            newPages();
    }

Add above method in EmpDetails Activity

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

Back button only finishes the current activity and brings the previous activity from back stack. In your newPages() method instead of creating the new Intent just call "finish();". This will end the current activity and start the previous one.

Parvaz Bhaskar
  • 1,367
  • 9
  • 29