简体   繁体   中英

ActionBarActivity back button to fragment

I have problem with the back button in action bar to fragment

my code fragment:

public class Server extends Fragment {
View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_server, container, false);
    Button server = (Button) view.findViewById(R.id.status);

    /** Button Check Status Server **/
    server.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Intent myIntent = new Intent(view.getContext(), ServerStatus.class);
            startActivityForResult(myIntent, 0);
            getActivity().finish();
        }
    });
    return view;
}
}

my code in activity:

public class ServerStatus extends ActionBarActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server_status);
}
}

My code in Fragment

@Override
public void onNavigationDrawerItemSelected(int position) {
    Fragment objFragment = null;
    switch (position) {
        case 0:
            objFragment = new Account();
            break;
        case 1:
            objFragment = new AllNews();
            break;
        case 2:
            objFragment = new Server();
            break;
        case 3:
            objFragment = new Account();
            break;
        case 4:
            objFragment = new Account();
            break;
        case 5:
            objFragment = new Account();
            break;
        case 6:
            objFragment = new About();
            break;
    }
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container,objFragment)
            .commit();
}

Every time I click the back button, the program always closes. I already tried to use:

getActionBar().setDisplayHomeAsUpEnabled(true);

and this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
    NavUtils.navigateUpFromSameTask(this);
    return true;
}
return super.onOptionsItemSelected(item);
}

Can anyone help me to fix my program?

EDIT: Concerning cross-activity-navigation , you should not use startActivityForResult() in that way. Try to use startActivity() instead. If you start an activity for a result, the calling activity waits for the onActivityResult() callback and should not be finished.

In the manifest file you should declare the fragment's activity as parent activity of ServerStatus to enable back navigation. You should not need NavUtils.

If you want to enable fragment navigation within an activity , you have to add your fragment transactions to the backstack: getFragmentManager().beginTransaction().addToBackStack(null).replace(...).commit();

Then you have to call getFragmentManager().popBackStack() in onOptionsItemSelected() to enable back navigation for the action bar :

@override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
    getFragmentManager().popBackStack();
    return true;
}
return super.onOptionsItemSelected(item);
}

You may have to override onBackPressed() to enable back navigation for the back button :

@override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() >= 1) {
         getFragmentManager().popBackStack(); // return to previous fragment
    }
    else {
        super.onBackPressed(); // Exit application when no fragment is on the backstack
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM