2017-07-18 58 views
0

當我改變我的手機的方向,文本內容不會保存和默認文本放在textview中,在我的Android清單中我的活動自助食堂我有這個方向與片段,文本不保存

android:configChanges="keyboardHidden|orientation" 

食堂

public class Cafeteria extends BaseActivity { 

private SectionsPagerAdapter mSectionsPagerAdapter; 


private ViewPager mViewPager; 

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

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 


    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.menu_cafeteria, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position){ 
      case 0: 
       Oferta oferta = new Oferta(); 
       return oferta; 
      case 1: 
       MenuCafe men = new MenuCafe(); 
       return men; 
      case 2: 
       Carta carta = new Carta(); 
       return carta; 
      default: 
       return null; 
     } 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return getString(R.string.ofertas); 
      case 1: 
       return getString(R.string.menu); 
      case 2: 
       return getString(R.string.carta); 
     } 
     return null; 
    } 
} 

MenuCafe

public class MenuCafe extends Fragment{ 
    private RecyclerView recyclerView; 
    private LinearLayoutManager linearLayoutManager; 
    private AdaptadorMenu adapter; 
    private List<Menu> menu; 




    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_menu, container, false); 
     recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_menu); 
     menu = new ArrayList<>(); 
     load_data_from_server(0); 
     linearLayoutManager = new LinearLayoutManager(getContext()); 
     recyclerView.setLayoutManager(linearLayoutManager); 
     adapter = new AdaptadorMenu(getContext(),menu); 
     recyclerView.setAdapter(adapter); 
     return rootView; 
    } 
} 

謝謝你的幫助

+0

你可以分享你的佈局文件(S)? –

回答

0

嘗試在Android清單文件中給這個

android:screenOrientation="portrait" 
0
  • 必須保存文本內容,因爲當旋轉屏幕,你的活動將重置機器人的生命週期,與第一次打開相同。
  • 瞭解如何在這裏保存狀態:Losing data when rotate screen