public class FragmentActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment class="com.example.hellopeacefullworld.FragmentList" android:id="@+id/fragment_list" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" > </fragment> <fragment class="com.example.hellopeacefullworld.FragmentDetail" android:id="@+id/fragment_detail" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" > </fragment> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment class="com.example.hellopeacefullworld.FragmentList" android:id="@+id/fragment_list" android:layout_width="match_parent" android:layout_height="match_parent" > </fragment> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment class="com.example.hellopeacefullworld.FragmentDetail" android:id="@+id/fragment_detail" android:layout_width="match_parent" android:layout_height="match_parent" > </fragment> </LinearLayout>
public class FragmentDetailActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_detail_activity); Bundle extras = getIntent().getExtras(); String s = extras.getString("selectedValue"); WebView viewer = (WebView) findViewById(R.id.webView1); viewer.loadUrl(s); } }
public class FragmentList extends ListFragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String[] values = new String[] { "http://mobile.tutsplus.com/tutorials/mobile-design-tutorials/80s-phone-app-slicing/", "http://mobile.tutsplus.com/tutorials/corona/create-a-brick-breaker-game-with-the-corona-sdk-game-controls/", "http://mobile.tutsplus.com/articles/news/best-of-tuts-in-february-2011/"}; ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } @Override public void onListItemClick(ListView l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); FragmentDetail fragment = (FragmentDetail)getFragmentManager().findFragmentById(R.id.fragment_detail); if (fragment != null && fragment.isInLayout()) { fragment.goToLink(item); } else { Intent intent = new Intent(getActivity().getApplicationContext(), FragmentDetailActivity.class); intent.putExtra("selectedValue", item); startActivity(intent); } } }
public class FragmentDetail extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_detail, container, false); return view; } public void goToLink(String item){ WebView viewer = (WebView)getView().findViewById(R.id.webView1); viewer.loadUrl(item); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<activity android:name=".FragmentDetailActivity" android:configChanges="orientation|screenSize|keyboardHidden"></activity>
public class FragmentDetailActivity extends Activity { String currentLink; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_detail_activity); Bundle extras = getIntent().getExtras(); currentLink = extras.getString("selectedValue"); WebView viewer = (WebView) findViewById(R.id.webView1); viewer.loadUrl(currentLink); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Intent intent = new Intent(this, FragmentActivity.class); intent.putExtra("selectedValue", currentLink); startActivity(intent); } } }
public class FragmentActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment); FragmentDetail fragment = (FragmentDetail)getFragmentManager().findFragmentById(R.id.fragment_detail); Bundle extras = getIntent().getExtras(); if(extras != null){ String link = extras.getString("selectedValue"); if(link !=null && fragment != null && fragment.isInLayout()){ fragment.goToLink(link); } } } }
Source: https://habr.com/ru/post/167315/
All Articles