简体   繁体   中英

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. The 3 fragments load a layout with webview (3 differents links, of course). 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. How can i do that ? Thanks for the help !!

Activity Main 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

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

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

<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 .

Programmatically create all the fragments probably in your onCreate and in your item selected method

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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM