调用Fragment会清除startActivity()上的应用程序视图

时间:2018-08-21 00:53:33

标签: android android-fragments

我是Android开发的新手,并已向“导航”菜单添加了一个附加项,当startActivity中的onActivityCreated时,该菜单可成功打开外部浏览器窗口,但清除应用程序屏幕(导航菜单除外)被执行。

enter image description here

尽管已为此工作了几天,但我仍未取得任何进展,因此,我现在寻求帮助。对于有更多经验的人来说,这可能是一个简单的问题。我已经花了一个星期的时间在SO,YouTube和Google开发人员课程中寻找其他所有答案,以解决这个问题,所以现在我寻求帮助。

WebsiteFragment.java

public class WebsiteFragment extends Fragment {
    View myFragment;

    RecyclerView websiteList;
    LinearLayoutManager layoutManager;

    private static final String COMMON_TAG="CombinedLifeCycle";
    private static final String ACTIVITY_NAME =WebsiteFragment.class.getSimpleName();
    private static final String TAG=ACTIVITY_NAME;

    public static WebsiteFragment newInstance() {
        WebsiteFragment websiteFragment = new WebsiteFragment();
        return websiteFragment;    
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable     ViewGroup container, @Nullable Bundle savedInstanceState) {
        //Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_website, container, false);

        Log.i(TAG,ACTIVITY_NAME+" onCreateView");

        websiteList = (RecyclerView)view.findViewById(R.id.websiteList);
        layoutManager = new LinearLayoutManager(getActivity());
        websiteList.setHasFixedSize(true);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse("https://english-alps.com"));
        // App Window is cleared??
        getActivity().startActivity(i);
        Log.i(TAG,ACTIVITY_NAME+" onActivityCreated");    
    }
}

fragment_webiste.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WebsiteFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/websiteList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </androidx.recyclerview.widget.RecyclerView>

</FrameLayout>

编辑21-08-2018

添加了以下代码来尝试保存Fragment状态,但会产生错误:

  

java.lang.NullPointerException:尝试从null对象引用的字段“ int androidx.fragment.app.Fragment.mIndex”中读取。

我在getFragmentManager的参数上遇到了麻烦。 应如何初始化getFragmentManager的变量?

public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState!=null){
        Log.i(TAG,ACTIVITY_NAME+" calling onSaveInstanceState");
        onSaveInstanceState(savedInstanceState);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getFragmentManager().putFragment(outState,TAG,myFragment);
    Log.i(TAG,ACTIVITY_NAME+" onSaveInstanceState");
}

编辑#2 21-08-2018

Home.Java ,它将WebsiteFragment添加到活动

public class Home extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment selectedFragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.action_category:
                        selectedFragment = CategoryFragment.newInstance();
                        break;
                    case R.id.action_ranking:
                        selectedFragment = RankingFragment.newInstance();
                        break;
                    case R.id.action_website:
                        selectedFragment = WebsiteFragment.newInstance();
                        break;
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_layout, selectedFragment);
                transaction.addToBackStack(String.valueOf(R.id.frame_layout)); /* 21.08.2018 */
                transaction.commit();
                return true;
            }

        });
        setDefaultKeyMode();

    }
}

编辑#3 21-08-2018 activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Home">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:animateLayoutChanges="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"></FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@drawable/nav_item_color_state"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:menu="@menu/bottom_menu"
    />
</RelativeLayout>  

0 个答案:

没有答案