簡體   English   中英

將字符串從活動傳遞到片段

[英]Passing String from an activity to Fragment

我試圖將一個字符串從一個活動傳遞到一個片段,以便稍后將它用作片段中其他內容的鍵,但它返回空值。 我究竟做錯了什么? 這是我的片段所在的活動代碼

import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class UserPage extends AppCompatActivity {

    String username;

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

        //this bit works fine, but later I also want to take string username and pass it to fragment
        username = getIntent().getExtras().getString("username");
        TextView welcome = findViewById(R.id.welcome_text);
        welcome.setText("Welcome, " + username);
    }

    public void flipTab(View v) {
        Fragment fragment = null;

        switch(v.getId()) {
            case R.id.btn_profile:
                fragment = new MyProfileFragment();
                break;
            case R.id.btn_edit:
                //another fragment here later
                break;
            case R.id.btn_colleges:
                fragment = new CollegeFragment();
                break;
        }
        //here I want to take the String username that came from the other activity and pass it to fragment
        Bundle args = new Bundle();
        args.putString("username", username);
        fragment.setArguments(args);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fr, fragment);
        ft.commit();
    }
}

這是我遇到問題的片段

import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MyProfileFragment extends Fragment {

    private String text;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View myFragmentView = inflater.inflate(R.layout.fragment_my_profile, container, false);

        TextView example = myFragmentView.findViewById(R.id.ex);

        if(getArguments() != null) {
            text = getArguments().getString("username");
        } else {
            example.setText(text);
            System.out.println("String=" + text);
        }

        Button signOut = myFragmentView.findViewById(R.id.user_sign_out);
        signOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Logged out");
                logOut();
            }
        });

        return myFragmentView;
    }

    public void logOut() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
    }

}

我是碎片的新手,如果我忽略了一些非常明顯的東西,我很抱歉。 但是我嘗試了一些事情,無論如何它們似乎都返回 null。 任何幫助,將不勝感激

有兩種方法,一種是使用 Bundle,另一種是在片段調用方法中傳遞參數

例子

按捆綁

活動中

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

並在 Fragment 任務中

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}

暫無
暫無

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

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