When you press the button back, nothing happens?



  • There are activists, fragments, fragments and a back button (DisplayHomeAsUpEnabled). The icon is displayed, but nothing happens when you press?

    Code activate:

    public class MainActivity extends ActionBarActivity implements ExpandableListView.OnChildClickListener, View.OnClickListener, FragmentManager.OnBackStackChangedListener {
        private static FragmentManager mManager;
        Button button1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
        button1 = (Button) findViewById(R.id.button1);
        mManager = getSupportFragmentManager();
    
        getSupportActionBar().setIcon(R.drawable.ic_launcher);
    
        getSupportFragmentManager().addOnBackStackChangedListener(this);
    
    
    
    
            button1.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Fragment fragment = null;
    
    
                    mManager.findFragmentById(R.id.fragmentContainer);
                    if (fragment == null) {
                        fragment = new sovety_Fragment();
                        mManager.beginTransaction()
                                .add(R.id.fragmentContainer, fragment)
                                .addToBackStack(null)
                                .commit();
                    }
    
                }
    
    
            });
    
    }
    
    
    
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
    
    
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    
        return false;
    }
    
    @Override
    public void onClick(View v) {
    
    }
    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        onBackStackChanged();
    }
    
    
    public void onBackStackChanged() {
        int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
        getSupportActionBar().setDisplayHomeAsUpEnabled(backStackEntryCount > 0);
    }
    

    }

    Code after change:

    package com.whitestar.topcar;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.ExpandableListView;

    public class MainActivity extends ActionBarActivity implements ExpandableListView.OnChildClickListener, View.OnClickListener, FragmentManager.OnBackStackChangedListener {
    private static FragmentManager mManager;
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

        button1 = (Button) findViewById(R.id.button1);
        mManager = getSupportFragmentManager();
    
        getSupportActionBar().setIcon(R.drawable.ic_launcher);
    
        getSupportFragmentManager().addOnBackStackChangedListener(this);
    
    
            button1.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Fragment fragment = null;
    
    
                    mManager.findFragmentById(R.id.fragmentContainer);
                    if (fragment == null) {
                        fragment = new sovety_Fragment();
                        mManager.beginTransaction()
                                .add(R.id.fragmentContainer, fragment)
                                .addToBackStack(null)
                                .commit();
                    }
    
                }
    
    
            });
    
    }
    
    
    
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == android.R.id.home) {
                onBackPressed();
    
                if(id == android.R.id.home)
                {
                    int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
                    if(backStackEntryCount > 0) {getSupportActionBar().setDisplayHomeAsUpEnabled(true);}
                    else { getSupportActionBar().setDisplayHomeAsUpEnabled(false);  }
                    onBackPressed();
                }  }
    
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    
        return false;
    }
    
    @Override
    public void onClick(View v) {
    
    }
    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        onBackStackChanged();
    }
    
    
    public void onBackStackChanged() {
        int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
        getSupportActionBar().setDisplayHomeAsUpEnabled(backStackEntryCount > 0);
    }
    

    }



  • You've forgotten to process the shooter again:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
    int id = item.getItemId();
    ...
    if(id == android.R.id.home)
    {
        int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
        if(backStackEntryCount > 0) {getSupportActionBar().setDisplayHomeAsUpEnabled(true);}
        else { getSupportActionBar().setDisplayHomeAsUpEnabled(false);  }
        onBackPressed();
        return true;
    }
    
    
    return super.onOptionsItemSelected(item);
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2