簡體   English   中英

方向改變時片段刷新的活動

[英]activity with fragments refreshing on orientation change

在任何人說這看起來像是重復的帖子之前..讓我解釋一下,我已經搜索了很多以找到解決我的問題的方法,但是還沒有找到任何可以解決我的問題的方法..

我正在使用帶有片段和抽屜中菜單的抽屜式布局,它會更改活動菜單中的片段。.每個片段在layout文件夾中有兩個布局,一個用於肖像,而在layout-land文件夾中一個。.當第一次打開活動時,它將加載默認情況下,菜單選擇片段上的onCreate()第一個片段已更改,但更改原始位置時,將再次顯示第一個片段,並在layout-land文件夾中顯示了橫向布局,這意味着刷新了活動。

如果我使用android:configChanges="orientation與活動..問題仍然存在,如果我使用android:configChanges="orientation|screenSize" ,然后問題解決,但在景觀布局layout-land不顯示,而不是顯示在縱向布局layout文件夾

主要活動

<?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>

家庭課

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");

 }



}

我認為您應該在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