简体   繁体   English

方向改变时片段刷新的活动

[英]activity with fragments refreshing on orientation change

Before any one says this looks like a duplicate post .. let me explain a bit, i have search alot to find the solution to my problem but haven't found any solution that can cater my problem .. 在任何人说这看起来像是重复的帖子之前..让我解释一下,我已经搜索了很多以找到解决我的问题的方法,但是还没有找到任何可以解决我的问题的方法..

i am using drawer layout with fragments and menu in drawer which changes the fragments on menu selection in activity..every fragment have 2 layout one for portrait in layout folder and one in layout-land folder.. when activity is first open it loads the first fragment by default in onCreate() on menu selection fragment is change but when origination is change is shows the first fragment again with landscape layout in layout-land folder which means activity is refreshed.. 我正在使用带有片段和抽屉中菜单的抽屉式布局,它会更改活动菜单中的片段。.每个片段在layout文件夹中有两个布局,一个用于肖像,而在layout-land文件夹中一个。.当第一次打开活动时,它将加载默认情况下,菜单选择片段上的onCreate()第一个片段已更改,但更改原始位置时,将再次显示第一个片段,并在layout-land文件夹中显示了横向布局,这意味着刷新了活动。

if i used android:configChanges="orientation with activity .. the problem persist if i uses android:configChanges="orientation|screenSize" then problem is resolved but landscape layout in layout-land is not shown instead shows the portrait layout in layout folder 如果我使用android:configChanges="orientation与活动..问题仍然存在,如果我使用android:configChanges="orientation|screenSize" ,然后问题解决,但在景观布局layout-land不显示,而不是显示在纵向布局layout文件夹

Main activity 主要活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Drawer"
android:background="@mipmap/home_background"

tools:context="com.example.minhasoftphp.radius.Home">



    <FrameLayout
        android:id="@+id/fragmentHolder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



 <LinearLayout
  android:layout_width="match_parent"
  android:layout_gravity="start"
  android:layout_marginEnd="-65dp"
  android:layout_marginRight="-65dp"
  android:layout_height="match_parent">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

 <include layout="@layout/drawer_menu"/>

 </ScrollView>

  </LinearLayout>

  </android.support.v4.widget.DrawerLayout>

home.class 家庭课

public class Home extends AppCompatActivity {

private boolean Toggletitle ;
private DrawerLayout mDrawerlayout ;
private ActionBarDrawerToggle mToggle;

private String Current_Fragment = "home" ;


@Override
protected void onResume() {
    super.onResume();
    loadFragment(new home());
    getSupportActionBar().setTitle(R.string.drawerclosed);
}


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




    mDrawerlayout = (DrawerLayout) findViewById(R.id.Drawer) ;


     mToggle = new ActionBarDrawerToggle
    (this,mDrawerlayout,R.string.draweropen,R.string.drawerclosed) ; 

    mDrawerlayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


  //  addmenufragments(Current_Fragment);
  //  loadFragment(new home());
}




@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(mToggle.onOptionsItemSelected(item))
    {
        if(!Toggletitle || !mDrawerlayout.isDrawerOpen(GravityCompat.START))
        {
            Toggletitle = true ;
            getSupportActionBar().setTitle(R.string.draweropen);
        }

        else if(Toggletitle || mDrawerlayout.isDrawerOpen(GravityCompat.START))
        {
            Toggletitle = false ;
            getSupportActionBar().setTitle(R.string.drawerclosed);
        }

        return true ;

    }

    return super.onOptionsItemSelected(item);
}





public void menu_click(View v)
{

    switch (v.getId())
    {
        case R.id.home :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Home");
            Current_Fragment = "Home";
            loadFragment(new home());
            break;

        case R.id.profile :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Profile");
            Current_Fragment = "Profile";
            loadFragment(new profile());
            break;

        case R.id.sell :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Sell property");
            Current_Fragment = "Sell";
            break;

        case R.id.trans :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Transactions");
            Current_Fragment = "Transactions";
            break;

        case R.id.events :

            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Events");
            Current_Fragment = "Events";
            break;

        case R.id.share :
            mDrawerlayout.closeDrawer(Gravity.LEFT);
            getSupportActionBar().setTitle("Events");
            Current_Fragment = "Events";
            break;
    }

}

public void Onclick(View view)
{
    Intent theIntent = new Intent(this, Catagory.class);


    switch (view.getId())
    {
        case R.id.bharia :

            theIntent.putExtra("category", "Baharia");
            startActivity(theIntent);

            break;


        case R.id.DHAcity :

            theIntent.putExtra("category", "DHA city");
            startActivity(theIntent);

            break;

        case R.id.DHAlhr :
            theIntent.putExtra("category", "DHA Lahore");
            startActivity(theIntent);

            break;

    }

}


private void loadFragment(Fragment fragment) {
// create a FragmentManager
    FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the Fragment
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
    fragmentTransaction.replace(R.id.fragmentHolder, fragment);
    fragmentTransaction.commit(); // save the changes
}


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putString("fragment", Current_Fragment);
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);


}

 @Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);
    Current_Fragment = savedInstanceState.getString("fragment");

 }



}

I think you should use onConfigurationChanged in the Layout Activity class and write an if/else condition for the Orientations. 我认为您应该在Layout Activity类中使用onConfigurationChanged并为Orientations编写if / else条件。

@override  
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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