[英]Load fragments when call it and keep when switch to another fragment/activity
I was looking here some ways to do what i want, i tried some codes but nothen works. 我在这里寻找一些方法来做我想做的事,我尝试了一些代码,但是没什么用。 I have a app with bottom bar (3 itens), and i have one fragment for each one.
我有一个带有底部栏(3个itens)的应用程序,每个都有一个片段。 The 3 fragments load a layout with webview (3 differents links, of course).
这3个片段会加载带有Webview的布局(当然还有3个不同的链接)。 What i want is switch between fragment and keep it in the background, beause when I leave fragment 1 to fragment 2, and i back to fragment 1, the fragment 1 load again, and i just want to keep it load, and the others fragments too.
我想要的是在片段之间切换并保持在后台,因为当我将片段1留给片段2,而我又回到片段1时,片段1再次加载,而我只想保持它的加载,其他片段太。 How can i do that ?
我怎样才能做到这一点 ? Thanks for the help !!
谢谢您的帮助 !!
Activity Main XML 活动主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"/>
Main Activity Java 主要活动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();
}
Fragment Example Java 片段示例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;
}
Fragment XML 片段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"/>
Instead of using replace everytime in your onNavigationItemSelected, use show and hide . 与其在onNavigationItemSelected中每次都使用replace ,不如使用show and hide 。
Programmatically create all the fragments probably in your onCreate and in your item selected method 以编程方式在onCreate和项目选择方法中创建所有片段
instead of this 代替这个
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
you do 你做
getSupportFragmentManager().beginTransaction().show(R.id.content, selectedFragment).commit();
and similarly for hiding 以及类似的隐藏
getSupportFragmentManager().beginTransaction().hide(R.id.content, selectedFragment).commit();
In this case, it will created only once(the first time), and subsequently will be just shown and hidden (think in terms of VISIBLE and GONE , not saying its the same , but similar) 在这种情况下,它将仅创建一次(第一次),随后将仅被显示和隐藏(考虑到VISIBLE和GONE , 不要说相同 ,而是类似)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.