0
  • 最佳答案
    replaceFragment(new RightFragment());你的fragment在这里啊,自己new出来,保存act中,不就有了?
    
    1139423796017500160  评论     打赏       断点-含光君
    • ccTyL  回复 @断点-含光君
      就直接new 出来就完了吗。 那我这里引出一个问题,service有一节是关于绑定的,我直接new Service不就可以,那个Service为什么不可以直接new 出来而是要绑定呢
      ccTyL 2021-06-20 15:49   回复 1314408005793603584
    • 拉大锯  回复 @ccTyL
      有问题再发帖子,不要多个问题在一个帖子里。因为四大组建不是自己创建的,是由系统创建的。声明周期由系统管理。
      拉大锯 2021-06-20 18:48   回复 1153952789488054272
  • 完整代码如下;

    package com.example.fragmenttest;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "MainActivity";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            Button button = findViewById(R.id.btn);
            button.setOnClickListener(v->{
    
                replaceFragment(new AnotherRightFragment());
            });
    
    
            replaceFragment(new RightFragment());
    
            Fragment fragmentById = getSupportFragmentManager().findFragmentById(R.id.right_fragment);
            Log.d(TAG, "onCreate: ");
        }
    
        private void replaceFragment(Fragment fragment) {
    
    
            FragmentManager supportFragmentManager = getSupportFragmentManager();
    
            FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
    
            fragmentTransaction.addToBackStack(null);//添加到后退栈
            fragmentTransaction.replace(R.id.right_layout,fragment);
            fragmentTransaction.commit();
    
    
        }
    }
    


    RightLayout:

    package com.example.fragmenttest;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    public class RightFragment extends Fragment {
    
        private static final String TAG = "RightFragment";
    
        @Nullable
    
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
    
            View view = inflater.inflate(R.layout.right_fragment, container, false);
            return view;
    
    
        }
    
    
        public void test(){
            Log.d(TAG, "test: ");
        }
    }
    
    


    1314408005793603584  评论     打赏       ccTyL
    • 你在哪里创建的fragment呀?

      1153952789488054272  评论     打赏       拉大锯
      • ccTyL  回复 @拉大锯
        是要先replaceFragment(new RightFragment());才能获取到吗
        ccTyL 2021-06-20 10:53   回复 1314408005793603584
      • ccTyL  回复 @拉大锯
        麻烦大佬帮我看看,我上面给了完整的代码
        ccTyL 2021-06-20 10:58   回复 1314408005793603584
      • 拉大锯  回复 @ccTyL
        你想在Activity调用fragment里的test方法吗?
        拉大锯 2021-06-20 11:35   回复 1153952789488054272
      • ccTyL  回复 @拉大锯
        是,想activity 和fragment 互相调用
        ccTyL 2021-06-20 12:34   回复 1314408005793603584
      • 拉大锯  回复 @ccTyL
        前面的同学已经说了,持有引用即可调用。
        拉大锯 2021-06-20 14:22   回复 1153952789488054272
      • ccTyL  回复 @拉大锯
        好的
        ccTyL 2021-06-20 15:50   回复 1314408005793603584
    相关问题
    2023-11-21 15:11 13 2
    陈钱罐16 · 领券联盟
    2023-11-11 18:09 23 8
    哈哈android · AOSP
    2023-11-10 15:52 13 12
    2023-11-10 00:26 22 10
    zhanghengze · Android
    2023-10-26 21:52 22 8