簡體   English   中英

Android Studio - Bundle 不在 Fragment 之間傳遞數據

[英]Android Studio - Bundle not passing data between Fragments

我正在開發一個應用程序,它總共包含 5 個片段,並與底部導航菜單相連。 為了簡單起見,我將使用其中的 2 個作為示例。

package com.example.ssresilience;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GoalsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GoalsFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private Button fg_goals_socialize;
    private Button fg_goals_study;
    private Button fg_goals_exercise;
    private Button fg_goals_setgoal;
    private TextView fg_goals_placeholder;

    private int check = 0;

    public GoalsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GoalsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GoalsFragment newInstance(String param1, String param2) {
        GoalsFragment fragment = new GoalsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

        fg_goals_socialize = (Button)rootView.findViewById(R.id.fg_goals_socialize);
        fg_goals_socialize.setOnClickListener(this::onClick);
        fg_goals_study = (Button)rootView.findViewById(R.id.fg_goals_study);
        fg_goals_study.setOnClickListener(this::onClick);
        fg_goals_exercise = (Button)rootView.findViewById(R.id.fg_goals_exercise);
        fg_goals_exercise.setOnClickListener(this::onClick);
        fg_goals_setgoal = (Button)rootView.findViewById(R.id.fg_goals_setgoal);
        fg_goals_setgoal.setOnClickListener(this::onClick);
        fg_goals_placeholder = (TextView)rootView.findViewById(R.id.fg_goals_placeholder);

        return rootView;
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "ResourceAsColor", "UseCompatLoadingForColorStateLists", "NonConstantResourceId", "SetTextI18n"})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fg_goals_socialize:
                fg_goals_placeholder.setText("• Talk to 3 or more people, other than your family.\n\n" +
                        "• Engage in phone or video calls from any device.\n\n" +
                        "• Exit your house for more than 2 hours.\n\n" +
                        "• Meet new people online or via your phone.");
                fg_goals_socialize.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect));
                fg_goals_socialize.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text));
                fg_goals_study.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect_reset));
                fg_goals_study.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text_reset));
                fg_goals_exercise.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect_reset));
                fg_goals_exercise.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text_reset));
                check = 1;
                break;
            case R.id.fg_goals_study:
                fg_goals_placeholder.setText("• Study for 1 or more hours today.\n\n" +
                        "• Work for at least 1 hour on your projects.\n\n" +
                        "• Talk to friends or relatives about your projects.\n\n" +
                        "• Dedicate study time for the course of your liking.");
                fg_goals_socialize.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect_reset));
                fg_goals_socialize.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text_reset));
                fg_goals_study.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect));
                fg_goals_study.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text));
                fg_goals_exercise.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect_reset));
                fg_goals_exercise.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text_reset));
                check = 2;
                break;
            case R.id.fg_goals_exercise:
                fg_goals_placeholder.setText("• Dedicate more than 1 hour on physical exercise.\n\n" +
                        "• Participate in physical exercises with friends or others.\n\n" +
                        "• Achieve your physical exercise-related goal.\n\n" +
                        "• Track your fitness-related progress in any way.");
                fg_goals_socialize.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect_reset));
                fg_goals_socialize.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text_reset));
                fg_goals_study.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect_reset));
                fg_goals_study.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text_reset));
                fg_goals_exercise.setBackgroundDrawable(getResources().getDrawable(R.drawable.buttoncolor_rect));
                fg_goals_exercise.setTextColor(getResources().getColorStateList(R.color.buttoncolor_text));
                check = 3;
                break;
            default:
                break;
        }
        switch (v.getId()) {
            case R.id.fg_goals_setgoal:
                if(check == 0) {
                    Toast.makeText(getActivity(), "Please select one of the 3 Goals!",
                            Toast.LENGTH_LONG).show();
                }
                if(check == 1) {
                    Bundle args = new Bundle();
                    args.putInt("goal", 1);
                    ProgressFragment newFragment = new ProgressFragment();
                    newFragment.setArguments(args);
                    Toast.makeText(getActivity(), "Current Goal: Socialize More",
                            Toast.LENGTH_LONG).show();
                }
                if(check == 2) {
                    Bundle args = new Bundle();
                    args.putInt("goal", 2);
                    ProgressFragment newFragment = new ProgressFragment();
                    newFragment.setArguments(args);
                    Toast.makeText(getActivity(), "Current Goal: Enhance Study Motives",
                            Toast.LENGTH_LONG).show();
                }
                if(check == 3) {
                    Bundle args = new Bundle();
                    args.putInt("goal", 3);
                    ProgressFragment newFragment = new ProgressFragment();
                    newFragment.setArguments(args);
                    Toast.makeText(getActivity(), "Current Goal: Physical Exercise",
                            Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
    }
}

在上面的GoalsFragment.java 中,有2 個開關方法,一個突出顯示按鈕並分配檢查變量,而下面的另一個包含確定按下哪個按鈕的if 語句。 這兩個工作完美無缺,所以問題不存在。 根據按下的按鈕,在第二種切換方法中,我們有 3 個不同的,用於在按下“SET GOAL”按鈕時將數據傳遞給 Fragment ProgressFragment.java

package com.example.ssresilience;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ProgressFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ProgressFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public ProgressFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ProgressFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ProgressFragment newInstance(String param1, String param2) {
        ProgressFragment fragment = new ProgressFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

        // Get the Selected Goal data from the GoalsFragment
        Bundle b = getArguments();
        int goal = b.getInt("goal");
        System.out.println(goal);

        // Set the Progress Fragment Text according to the selected Goal
        TextView fg_progress_header2 = (TextView) rootView.findViewById(R.id.fg_progress_header2);
        if (goal == 1) {
            fg_progress_header2.setText("Socialize More");
        } if (goal == 2) {
            fg_progress_header2.setText("Enhance Study Motives");
        } if (goal == 3) {
            fg_progress_header2.setText("Physical Exercise");
        }

        return rootView;
    }
}

在第二個 Fragment 中,我試圖從目標片段.java中的 3 個之一中檢索目標數據,並相應地設置 TextViews 的文本。 出於測試目的,我包含了一個System.out.println(goal); ,返回0,表示沒有從第一個Fragment正確檢索到bundle數據。

我什至嘗試將捆綁包放在GoalsFragment.javaonCreateView方法上,以防萬一下面的代碼出錯,但它仍然不起作用。

有人可以幫我嗎? 我所做的一切似乎都不起作用...

先感謝您!

您需要通過 ViewModel 或片段偵聽器與多個片段進行通信

從你的GoalFragment

    Bundle result = new Bundle();
    result.putInt("goal", 1);
    getParentFragmentManager().setFragmentResult("progress", result);

在你的ProgressFragment

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getParentFragmentManager().setFragmentResultListener("requestKey", this, new FragmentResultListener() {
        @Override
        public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle) {
            // We use a String here, but any type that can be put in a Bundle is supported
            String result = bundle.getInt("goal");
            // Do something with the result
        }
    });
}

您可以查看文檔以了解詳細實現https://developer.android.com/guide/fragments/communicate#java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM