During Android development, I want to use onCreateView() in onActivityResult, but an error appears

Asked 2 years ago, Updated 2 years ago, 22 views

    public class FragmentE extends Fragment implements View.OnClickListener
{

private static final int EDIT_DATA_REQUEST_CODE = 1;

Button editMyInfo;
ToggleButton quick_view_switch;

private ListView listView;
private CustomAdapter adapter;
public DatabaseHelper dbHelper;
Context mContext;

String myName;
String myRank;
String myTel;
String myAddr;
String myEmail;

    //Trying to invoke this method externally.
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_setting, container, false);
    mContext = v.getContext();
    dbHelper = new DatabaseHelper(mContext);

    listView = (ListView) v.findViewById(R.id.setting_listView);
    adapter = new CustomAdapter();
    listView.setAdapter(adapter);

    editMyInfo = (Button)v.findViewById(R.id.editMyInfo);
    editMyInfo.setOnClickListener(this);

    quick_view_switch = (ToggleButton) v.findViewById(R.id.toggleButton1);
       // // quick_view_switch.setOnClickListener(this);

        return v;
    }

    @Override
    public void onResume(){
    super.onResume();

    Cursor mCursor = (Cursor)dbHelper.getAllData();

    mCursor.moveToFirst();

    myName = mCursor.getString(mCursor.getColumnIndexOrThrow(dbHelper.USER_TABLE_COLUMN_NAME));
    myRank = mCursor.getString(mCursor.getColumnIndexOrThrow(dbHelper.USER_TABLE_COLUMN_RANK));
    myTel = mCursor.getString(mCursor.getColumnIndexOrThrow(dbHelper.USER_TABLE_COLUMN_TEL));
    myAddr = mCursor.getString(mCursor.getColumnIndexOrThrow(dbHelper.USER_TABLE_COLUMN_ADDR));
    myEmail = mCursor.getString(mCursor.getColumnIndexOrThrow(dbHelper.USER_TABLE_COLUMN_EMAIL));

        adapter.add ("Name", myName);
        adapter.add ("number", myTel);
        adapter.add ("Status", myRank);
        adapter.add ("mail", myEmail);
        adapter.add ("Address", myAddr);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode==getActivity().RESULT_OK){
        switch (requestCode){
            case EDIT_DATA_REQUEST_CODE:
                Toast.makeText(mContext, "ELEL", Toast.LENGTH_SHORT).show();
                onDestroyView();
                onCreateView(); // I don't know what parameters to put in here.
                onStart();
                break;
        }
     } 
    }

    @Override
    public void onClick(View v) {
    Intent intent = new Intent(getActivity().getApplicationContext(),EditMyInfo.class);
    startActivityForResult(intent,EDIT_DATA_REQUEST_CODE);

      }
    }

How do I invoke a method called when an activity starts, such as the onCreate() method, from another method?

java android

2022-09-21 23:05

1 Answers

If the purpose is to regenerate fragments, use the replace() function as follows.

getSupportFragmentManager().beginTransaction()
    .replace(R.id.content, XXXFragment.newInstance())
    .commit();

In addition, life cycle functions such as onCreate(), onDestroy(), onCreateView(), onDetroyView(), onStart(), onStop()... are functions called by the system. In other words, there is no need to call a function directly by logic, and it is meaningless to call it that way. For example, when onDestroyView() is called by the system, calling onCreateView() within this function does not regenerate the fragment.

Refer to the activity and fragment article below when the life cycle function is called.


2022-09-21 23:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.