在Navigation Drawer活动中恢复片段状态

时间:2017-01-31 07:32:09

标签: java android android-studio android-fragments

我在修复NavigationDrawer活动中不同片段的状态时遇到问题。我有4个标签,调用4个不同的片段。当我切换回来时,我想恢复以前的状态。我一直在尝试但似乎无法在恢复状态时实现addToBackStack。以下代码再次重新加载片段。

MainActivity.java

public class Calenndar_Model {
    private String host, date,userid,color,autoid;

    //private double rating;

public Calenndar_Model() {
}

public Calenndar_Model(String host,String date,String userid,String color,String autoid) {
    this.host = host;
    this.date = date;
    this.userid = userid;
    this.color = color;
    this.autoid = autoid;

}

public String getHost() {
    return host;
}

public void setHost(String host) {
    this.host = host;
}

public String getDate() {

    return date;
}
public void setDate(String date) {

        this.date = date;
}

public String getUserid() {
    return userid;
}

public void setUserid(String userid) {
    this.userid = userid;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public String getAutoid() {
    return autoid;
}

public void setAutoid(String autoid) {
    this.autoid = autoid;
}

}

}

homeFragment.java

public class homeFragment扩展Fragment {

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    homeFragment fragment=new homeFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container,fragment);
    fragmentTransaction.commit();


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        homeFragment fragment=new homeFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.addToBackStack("home").commit();

    } else if (id == R.id.nav_gallery) {

        techNews fragment=new techNews();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.addToBackStack("tech").commit();

    } else if (id == R.id.nav_slideshow) {

        nepal fragment=new nepal();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_manage) {
        mobilePrice fragment=new mobilePrice();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.commit();


    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

TechNews.java

private RecyclerView rvItem;
private updateAdapter updateLatestPage;

public homeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    //setContentView(R.layout.activity_main);
    View rootview=inflater.inflate(R.layout.fragment_home,container,false);
    rvItem = (RecyclerView)rootview.findViewById(R.id.rvItem);
    rvItem.setHasFixedSize(true);

    final LinearLayoutManager manager = new LinearLayoutManager(getContext());
    rvItem.setLayoutManager(manager);
    String url = "http://www.gadgetsinnepal.com.np/wp-json/wp/v2/posts/?page=";
    updateLatestPage = new updateAdapter(rvItem, getContext(), url, manager);
    updateLatestPage.fetchAndPut();
    return rootview;
}

}

所有其他片段都相似。我该如何恢复每个片段的状态?

0 个答案:

没有答案