Android-试图将一些数据从Activity(不是MainActivity)传递到一个Fragment(由我的MainActivity创建)。可以吗?

问题描述 投票:0回答:2

最近我被卡住了!我有一个Activity(不是我的MainActivity,这不是我创建此Fragment的地方),我需要传递一些数据。我已经尝试通过使用getter的Bundle进行传递,但是出现相同的问题:“试图在Fragment中调用Bundle的那一行调用虚拟方法{...}。”这是一个新手,所以很抱歉,如果这是一个简单的问题,并且我听不懂,请在我代码的相关部分下面:

关于活动(不是主要活动):

public void save(){
    myGoal = spinnerGoals.getSelectedItem().toString();

    Bundle bundle = new Bundle();
    bundle.putString( "goal", myGoal);
    GoalFragment goalFragment = new GoalFragment();
    goalFragment.setArguments( bundle );

}

}

关于片段(我想在其中放置“目标”)

private TextView goal;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate( R.layout.fragment_goal, container, false );
    goal = view.findViewById( R.id.myGoalText );

    Bundle bundle = getArguments();
    if (bundle != null) {
        final String myGoal = bundle.getString( "goal" );
        goal.setText( myGoal);

    }
    return view;
}

}

在MainActivity(我在其中创建了片段):

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

    BottomNavigationView bottomNav = findViewById( R.id.bottom_navigation );
    bottomNav.setOnNavigationItemSelectedListener( navListener );

    getSupportFragmentManager().beginTransaction().replace( R.id.fragment_container, new HomeFragment() )
            .commit();

}


private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;

                    case R.id.nav_goal:
                        selectedFragment = new GoalFragment();
                        break;

                    case R.id.nav_info:
                        selectedFragment = new InfoFragment();
                        break;
                }

                getSupportFragmentManager().beginTransaction().replace( R.id.fragment_container, selectedFragment )
                        .commit();


                return true;
            }

};

}

请,我已经浪费了很多时间试图自己解决这个问题哈哈哈,如果有人可以帮助我,我将不胜感激!

android android-studio android-fragments android-activity android-fragmentactivity
2个回答
0
投票

在Fragment中创建要接收数据的方法,然后在Activity中调用该方法。

在活动中写此代码:

 Bundle bundle = new Bundle();
 bundle.putString("goal", myGoal);
 fragmentObj.sendData(bundle);

在片段中编写代码:

public void sendData(Bundle bundle){
      String myGoal = bundle.getString("goal");
}

0
投票

如果活动(不是主要活动)是从您的片段创建的,则可以使用

startActivityForResult()

活动结束后接收数据的方法,请详细阅读here

如果不是这种情况,则需要使用PreferencesRoom Database持久化(存储)数据>

© www.soinside.com 2019 - 2024. All rights reserved.