从主要活动到片段捆绑

时间:2018-06-25 12:16:53

标签: android

我遵循一些指南将参数从登录名传递到一个片段。 我具有这种结构(登录页面,用户区(仅包含一个片段和一些片段的主要活动) 我尝试这样做:

public class UserAreaActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{

ProgressDialog progressDialog;

@Override
public void onBackPressed() {


    //super.onBackPressed();
}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.example.anto.fitfix.R.layout.activity_user_area);






  Bundle b = getIntent().getExtras();

    final String name = b.getString("name");
    final String user = b.getString("id");
    final String email = b.getString("email");
    final String gender = b.getString("gender");
    final String photourl= b.getString("photo");
    final String birthday = b.getString("birthday");
    final String p="null";


    Log.d("nome",""+name);



   // loadFragment(new fragment_home());

    Fragment fragment = new Fragment();
    Bundle bundle = new Bundle();
    bundle.putInt("name", 123);
    fragment.setArguments(bundle);

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

    BottomNavigationView navigation = findViewById(R.id.navigationView);
    navigation.setOnNavigationItemSelectedListener(this);







    }
  @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        fragment_home fragment = null;

        switch (item.getItemId()) {
            case R.id.action_home:
                fragment = new fragment_home();
                break;

            case R.id.action_cron:
                fragment = new fragment_home();
                break;

            case R.id.action_face:
               // fragment = new NotificationsFragment();
                break;


        }

        return loadFragment(fragment);
    }
private boolean loadFragment(android.support.v4.app.Fragment fragment) {
    //switching fragment
    if (fragment != null) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
        return true;
    }
    return false;
}

}

这是片段

public class fragment_home extends Fragment {

    ProgressDialog progressDialog;
    String name,email,user,p;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //just change the fragment_dashboard
        //with the fragment you want to inflate
        //like if the class is HomeFragment it should have R.layout.home_fragment
        //if it is DashboardFragment it should have R.layout.fragment_dashboard
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        TextView tResult = (TextView) view.findViewById(R.id.tResult);
        TextView tMail = (TextView) view.findViewById(com.example.anto.fitfix.R.id.tMail);
        TextView tAge = (TextView) view.findViewById(R.id.tAge);


        Bundle bundle = this.getArguments();
        if (bundle != null) {

            int myInt = bundle.getInt("name");
            Log.d("name",""+myInt);
            tResult.setText(name);
        }





        BLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog = new ProgressDialog(getActivity());
                progressDialog.setMessage("wait..."); // Setting Message
                progressDialog.setTitle("Logout"); // Setting Title
                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
                progressDialog.show(); // Display Progress Dialog
                progressDialog.setCancelable(false);
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            disconnectFromFacebook();
                            Thread.sleep(1600);
                            Intent fbIntent = new Intent(getActivity(), LoginActivity.class);
                            getActivity().startActivity(fbIntent);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        progressDialog.dismiss();

                    }
                }).start();


            }
        });

        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    boolean success = jsonResponse.getBoolean("success");



                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        };

      /*  FbActivity registerRequest = new FbActivity(name, user, p, email, responseListener);
        RequestQueue queue = Volley.newRequestQueue(getActivity());
        queue.add(registerRequest);*/

        return view;
    }






}

我从login那里获取了名字,但是我无法通过片段部分。 有人可以给我建议吗?

1 个答案:

答案 0 :(得分:1)

将捆绑包传递给片段时,您不会创建Fragment类的对象,而是会创建您创建的片段的对象,即fragment_home < / p>

Fragment fragment = new Fragment();
 Bundle bundle = new Bundle();
 bundle.putInt("name", 123);
 fragment.setArguments(bundle);
 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new fragment_home()).commit();

用下面的代码更改上面的代码

 fragment_home fragment = new fragment_home();
    Bundle bundle = new Bundle();
    bundle.putInt("name", 123);
    fragment.setArguments(bundle);

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