I know this question has been asked multiple times but I cannot seem to find the answer to my problem.
I have tried the solution of this post: How to go to fragment from activity as shown below:
You need to created a class that extends FragmentActivity and start yourfragment there
public class MessagesFragmentActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new MessagesFragment ()).commit();}
}
}
Your fragment constructor.
public YourFragment() {
}
then from you calling activity, start your fragment activity in the normal way
Intent i = new Intent(YourActivity.this,MessagesFragment.class);
startActivity(i);
But it does not work for me as my activity extends AppCompatActivity, so I cannot extend FragmentActivity.
In this case, I tried creating a new activity which extends FragmentActivity and link an intent to this new activity instead, then this new activity would then move to the fragment.
Below are my codes:
In my Activity:
saveBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listName.getText().toString().equals("")) {
listName.requestFocus();
listName.setError("Required field");
} else {
if (listOwner.getText().toString().equals("")) {
listOwner.requestFocus();
listOwner.setError("Required field");
} else {
params.add(new BasicNameValuePair("userID", userid));
params.add(new BasicNameValuePair("Name", listName
.getText().toString()));
params.add(new BasicNameValuePair("OwnerName",
listOwner.getText().toString()));
params.add(new BasicNameValuePair("CompanyName",
companyName.getText().toString()));
params.add(new BasicNameValuePair("CompanyAddress",
companyAddress.getText().toString()));
params.add(new BasicNameValuePair("CompanyPhone",
companyNumber.getText().toString()));
for (int d = 0; d < CustomFieldsList.size(); d++) {
CustomFields field = CustomFieldsList.get(d);
CheckBox cb = (CheckBox) findViewById(field
.getFieldID());
if (cb.isChecked()) {
checkboxChecked.add(Integer.toString(field
.getFieldID()));
}
}
new SaveContact().execute();
Intent back = new Intent(AddContactListActivityOCR.this, Revert.class);
startActivity(back);
finish();
}
In my New Activity (Revert.java):
public class Revert extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new FragmentContactLists()).commit();}
}
}
However, it shows the following error in here:
What went wrong?
-------------------------------------EDIT--------------------------------------
After trying @saeed answer:
Although I imported "android.support.v4.app.Fragment;", it still shows this error.