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..
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
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.
@override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.