Here is the problem. I've got such layout for big screen devices
<LinearLayout
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lexzcq.diffsizes.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
android:id="@+id/list_fragment"
android:name="com.example.lexzcq.diffsizes.MainListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@android:layout/list_content"/>
<fragment
android:id="@+id/details_fragment"
android:name="com.example.lexzcq.diffsizes.DetailsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/details_fragment"/>
</LinearLayout>
</LinearLayout>
And here is my java code for activity
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements MainListFragment.onDetailFragmentGetText, DetailsFragment.OnSetDetailsText {
Fragment listFragment;
Fragment detailsFragment;
FragmentTransaction transaction;
TextView detailsText;
String fromListText = "";
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
if (getDiagonal () >= 7 && getResources ().getConfiguration ().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView (R.layout.avtivity_main_pads);
listFragment = new MainListFragment ();
transaction = getSupportFragmentManager ().beginTransaction ();
transaction.replace (R.id.list_fragment, listFragment)
.addToBackStack (null)
.commit ();
} else {
setContentView (R.layout.activity_main_vertical_phone);
listFragment = new MainListFragment ();
transaction = getSupportFragmentManager ().beginTransaction ();
transaction.replace (R.id.container, listFragment)
.addToBackStack (null)
.commit ();
}
}
public void onGetText (String s) {
detailsFragment = new DetailsFragment ();
transaction = getSupportFragmentManager ().beginTransaction ();
transaction.replace (R.id.container, detailsFragment)
.addToBackStack (null)
.commit ();
fromListText = s;
}
public void setDetailsText () {
detailsText = (TextView) findViewById (R.id.details_container);
detailsText.setText (fromListText);
}
private int getDiagonal () {
DisplayMetrics metrics = new DisplayMetrics ();
getWindowManager ().getDefaultDisplay ().getMetrics (metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
double diagonalInches = Math.sqrt ((widthInches * widthInches) + (heightInches * heightInches));
return (int) diagonalInches;
}
}
Every time I switch between articles in list fragment content of details fragment changes and old fragment adds to backstack (in onGetText () method) but when I'm pushing back button nothing happens. I've tried to override OnBackPressed method with replacing new fragment with old and with popBackStack () method,but it doesn't work. Can someone tell me how to work with backstack and two (or more) fragments?