[英]Load fragments when call it and keep when switch to another fragment/activity
我在这里寻找一些方法来做我想做的事,我尝试了一些代码,但是没什么用。 我有一个带有底部栏(3个itens)的应用程序,每个都有一个片段。 这3个片段会加载带有Webview的布局(当然还有3个不同的链接)。 我想要的是在片段之间切换并保持在后台,因为当我将片段1留给片段2,而我又回到片段1时,片段1再次加载,而我只想保持它的加载,其他片段太。 我怎样才能做到这一点 ? 谢谢您的帮助 !!
活动主XML
<LinearLayout
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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bandeira.thalisson.appExample.MainActivity">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"/>
主要活动Java
public class MainActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_item1:
selectedFragment = Fragment1.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
return true;
case R.id.navigation_item2:
selectedFragment = Fragment2.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
return true;
case R.id.navigation_item3:
selectedFragment = Fragment3.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
//*Iniciar um fragmento junto com o aplicativo
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content, Fragment1.newInstance());
transaction.commit();
}
片段示例Java
public class Google extends Fragment {
public WebView myWebView;
public static Google newInstance() {
Googlefragment = new Google();
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.webview, container, false);
myWebView = (WebView) rootView.findViewById(R.id.WebViewLayout);
myWebView.loadUrl("http://google.com/");
//*Ativar JavaScript
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//*Forçar links para abrir no WebView ao invés do navegador
myWebView.setWebViewClient(new WebViewClient());
return rootView;
}
片段XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/WebViewLayout"/>
与其在onNavigationItemSelected中每次都使用replace ,不如使用show and hide 。
以编程方式在onCreate和项目选择方法中创建所有片段
代替这个
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
你做
getSupportFragmentManager().beginTransaction().show(R.id.content, selectedFragment).commit();
以及类似的隐藏
getSupportFragmentManager().beginTransaction().hide(R.id.content, selectedFragment).commit();
在这种情况下,它将仅创建一次(第一次),随后将仅被显示和隐藏(考虑到VISIBLE和GONE , 不要说相同 ,而是类似)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.