0

I hope someone can help with my problem. Android back button works in some activities, and is broken in others. My MainActivity has other target acitivities to go to, depending on the options selected. As my app manages a map, when I click the Infowindow of a marker, it goes to the detail activity as desired, and when I click the Android back button, it goes back to the Main Activity as normal (not to be confused with the home button in the action bar, which I don't want to implement).

But when I tap the "Contact" or the "Terms and conditions" options, and I do the intent to start the Contact/Terms activities the same way I do with infowindows, it opens the activity as usual, but the back button doesn't work, it looks like it kinda reopens the Contact/Terms Activities and doesn't go back to Main Activity. I've searched many posts and I add my observations:

  1. I don't call finish() on my MainActivity, so I'm not purposely messing with the activity stack.
  2. I've tried putExtra("finishActivityOnSaveCompleted", true) as this post suggests, with no luck.
  3. I've tried making both activities the same (detail and Contact activity), just show a simple layout with no more imports than os.bundle, app.Activity, content.Intent, view.View and View.Window.
  4. I've tried firing the startActivity() function that calls Contact Activity from methods that override some function (detail Acvitity is called with a startActivity from a method that overrides a click event).
  5. I've tried putting/receiving some extra data from the Main to the Contact activity (that's how desperate I am).

I'm running out of options and I can't figure out what's wrong, and I know I shouldn't be implementing the back button function myself, since Android handles it for me. I'm targetting SDK version 16 and up in my manifest, I add a short version of the code that calls both activities:

public class MainActivity extends FragmentActivity implements OnNavigationListener, ClusterManager.OnClusterClickListener<MyItem>, ClusterManager.OnClusterInfoWindowClickListener<MyItem>, ClusterManager.OnClusterItemClickListener<MyItem>, ClusterManager.OnClusterItemInfoWindowClickListener<MyItem>
{
    .
    .
    .
    /*Some variables initialized*/
    .
    .
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        .
        .
        .
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                menuOpt=position;
                getData();
                //gridView.setVisibility(View.GONE);
                if(gridView.getVisibility() == View.VISIBLE) {
                    toggleView(gridView);
                }
            }
        });
    }
    public void getData(){
        String optString="";
        llMatVesp.setVisibility(View.GONE);
        switch(menuOpt){
        /* In this case the back button to MainActivity works, I manage action bar operations for a particular behaviour of the menu, all options in the switch do */
        case 1:  
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
            actionBar.setCustomView(null);
            getResults(); 
            break;
        /* In this case the back button of Contact Activity, that's supposed to return to MainActivity doesn't work */
        case 2: 
            Intent conIntent=new Intent(MainActivity.this, Contact.class);  startActivity(conIntent);
            break;
        default:break;
        }
    }
    /*this function brings data from db, add markers to the map, and animates to a certain point
    public void getResults(){
        .
        .
    }

    /* This functions extract the right data from the marker and make the intent that starts the detail Activity, where the back button works with no problems */
    @Override
    public void onClusterItemInfoWindowClick(MyItem item) {
        showDetail(item.getId(),item.getMenuOpt());
    }
    public void showDetail(String id, int menuOpt){
        Intent showDetail=new Intent(MainActivity.this, ShowDetail.class);
        showDetail.putExtra("menuOpt", menuOpt);
        showDetail.putExtra("icon", ic);        
        showDetail.putExtra("id", id);
        startActivity(showDetail);
    }
}

Contact activity has a form and sends email, but the other activity where back button doesn't work, TermsAndConditions activity, only displays a simple LinearLayout, and that's it, no fancy coding there.

Community
  • 1
  • 1
donluismx
  • 1
  • 2
  • I just discovered, when I tap the back button expecting to go back to MainActivity, it not only reopens Contact Activity, it also opens MainActivity getting focus and then gets paused automatically. So Contact is losing focus and regaining it so MainActivity get in the background but still active. Nasty bug. – donluismx Feb 13 '15 at 19:49

1 Answers1

0

I found a workaround this, you just need to override the back button default action, sending the user to Main Activity or any other you wish, but you will lose the activity back stack order if you came from a different activity. If your second activity uses webview, it will use chromium engine if the page loads with errors, in that case add these flags to the intent, otherwise ignore the line.

    @Override
public void onBackPressed() {
    Intent intent = new Intent(SecondActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
    return;
}