Java 如何从活动it';它显示在

Java 如何从活动it';它显示在,java,android,android-fragments,Java,Android,Android Fragments,我有TestsFragment,它从Fragment扩展而来。 在这个片段的布局中有一个TextView(id:TextView) 有一个名为main活动的Activity:此Activity包含一个显示片段的FrameLayout 我想设置文本视图TextonCreatemain活动,我想我需要查看此片段 我已经尝试了以下代码(OnCreate-MainActivity): 但它不起作用(没有错误,只是文本没有变化) 你能帮我吗? 如果您需要任何代码,请告诉我 主要活动: Fragment f

我有
TestsFragment
,它
Fragment
扩展而来。 在这个
片段的布局中有一个
TextView
(id:TextView) 有一个名为
main活动的
Activity
:此
Activity
包含一个显示
片段的
FrameLayout
我想设置文本视图Text
onCreate
main活动
,我想我需要查看此
片段

我已经尝试了以下代码(OnCreate-MainActivity):

但它不起作用(没有错误,只是文本没有变化)

你能帮我吗? 如果您需要任何代码,请告诉我

主要活动:

Fragment fragment = new TestsFragment();
View v = getLayoutInflater().inflate(R.layout.fragment_tests, null);
TextView tv = v.findViewById(R.id.textview);
tv.setText("text changed!");
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
Fragment testsFragment, signsFragment, practiceFragment, accountFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(navListener);


    if(savedInstanceState == null){
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new TestsFragment(), "TestsFragment").commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new SignsFragment()).commit();
        getSupportFragmentManager().executePendingTransactions();
    }

}

@Override
protected void onStart() {
    super.onStart();
    testsFragment = getSupportFragmentManager().findFragmentByTag("TestsFragment");
    if(testsFragment != null){
        TextView textView = testsFragment.getView().findViewById(R.id.textview);
        textView.setText("Text Changed!");
    }
}



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

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

            case R.id.nav_tests:
                selectedFragment = testsFragment;
                break;

            case R.id.nav_practice:
                selectedFragment = new PracticeFragment();
                break;

            case R.id.nav_account:
                selectedFragment = new AccountFragment();
                break;
        }

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
        return true;
    }
};
}
xml(布局):


解决方案

  • 当用户单击每个菜单项时,您总是创建一个新的片段,这不是一个好的用户体验,因为当用户在菜单项之间切换时,他们总是看到一个新屏幕,而不是他们离开之前看到的屏幕。例如,在
    SignsFragment
    中,有一个
    EditText
    允许用户输入他们的名字为“Bob”,当他们切换到
    AccountFragment
    并切换回
    SignsFragment
    时,
    EditText
    将为空(但用户希望看到“Bob”)

  • 为了防止这种行为,我们将使用
    addToBackStack()
    方法,因此
    FragmentManager
    将保留对所有创建片段的引用。如果片段在
    FragmentManager
    中不存在,它将创建一个新片段,否则,它将重用现有片段

MainActivity.java


此解决方案将在+1时起作用。OP还可以考虑使用它来大大简化活动和片段之间的通信等@Yaniv我没听明白你的意思。你能详细说明一下吗?或者你可以发布活动和片段的代码。这将帮助我更快地调查问题。@Yaniv让我调查一下你的代码以找到根本原因。@Yaniv请查看我的更新答案。哦,我没有看到。我需要走20分钟,我会试试的谢谢你!真正地你太棒了!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#007AFF" android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview_tests"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:paddingHorizontal="5dp">


</androidx.recyclerview.widget.RecyclerView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="text not changed"
    android:id="@+id/textview"/>

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    private static final String TAG_SIGNS_FRAGMENT = "TAG_SIGNS_FRAGMENT";
    private static final String TAG_TESTS_FRAGMENT = "TAG_TESTS_FRAGMENT";
    private static final String TAG_PRACTICE_FRAGMENT = "TAG_PRACTICE_FRAGMENT";
    private static final String TAG_ACCOUNT_FRAGMENT = "TAG_ACCOUNT_FRAGMENT";

    BottomNavigationView bottomNavigationView;

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

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
        bottomNavigationView.setSelectedItemId(R.id.nav_signs);
    }

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

            // Find the tag of fragment based on menu item position.
            switch (item.getItemId()) {
                case R.id.nav_signs:
                    tag = TAG_SIGNS_FRAGMENT;
                    break;
                case R.id.nav_tests:
                    tag = TAG_TESTS_FRAGMENT;
                    break;
                case R.id.nav_practice:
                    tag = TAG_PRACTICE_FRAGMENT;
                    break;
                case R.id.nav_account:
                    tag = TAG_ACCOUNT_FRAGMENT;
                    break;
            }

            // Find the fragment in FragmentManager.
            Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);

            // If the fragment is not existed, create a new instance of it, otherwise
            // use the existing one.
            if (fragment == null) {
                switch (tag) {
                    case TAG_SIGNS_FRAGMENT:
                        fragment = new SignsFragment();
                        break;
                    case TAG_TESTS_FRAGMENT:
                        fragment = new TestsFragment("text changed!");
                        break;
                    case TAG_PRACTICE_FRAGMENT:
                        fragment = new PracticeFragment();
                        break;
                    case TAG_ACCOUNT_FRAGMENT:
                        fragment = new AccountFragment();
                        break;
                }
            }
            // Show the fragment on screen.
            showFragment(fragment, tag);
            return true;
        }
    };

    private void showFragment(Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment, tag)
                .addToBackStack(tag)
                .commit();
        getSupportFragmentManager().executePendingTransactions();
    }
}