简体   繁体   English

调用时加载片段,并在切换到另一个片段/活动时保留

[英]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) 在这种情况下,它将仅创建一次(第一次),随后将仅被显示和隐藏(考虑到VISIBLEGONE不要说相同 ,而是类似)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 活动开始时加载片段 - load fragment when activity starts Map 片段在 Android Studio 之间切换时不隐藏片段 - Map fragment is not hidden when switch between fragments Android Studio 当AsyncTask完成活动时,调用片段方法 - Call fragment method when AsyncTask finish on activity 从第二个Fragment的recyclerView中选择项目时如何切换到第一个Fragment? 两个片段都在带有 viewPager2 的 tablayout 中 - How to switch to the first fragment when selecting an item from the recyclerView of second Fragment? Both fragments are in tablayout with viewPager2 使用导航切换到另一个片段时保存片段数据 - Save Fragment data when switch to another fragment using Navigation 单击浮动操作按钮时,片段与另一个片段重叠 - Fragments overlaps another fragment when clicking on floating action button 当我尝试从片段切换到活动时,应用崩溃 - App crashes when I try to switch from fragment to activity 从另一个片段返回时,主活动中的映射片段不可见 - Map fragment in main activity not visible when returning from another fragment 调用另一个片段时出现ListAdapter错误 - ListAdapter error when call another fragment 尝试显示不在菜单中的片段时,导航抽屉活动项目中的片段重叠 - Fragments overlapping in Navigation Drawer Activity project when trying to display a fragment that isn't in the menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM