Home > database >  Convert extends AppCompatActivity to extends Fragments
Convert extends AppCompatActivity to extends Fragments

Time:08-21

Hello I was wondering how I would convert my activity to a fragment activity.

I am currently using a bottom navigation for my android app which was implemented later on my project.

When I put my activity into a switch case function it says that it needs to be a Fragment and not a AppCompatActivity.

Here is my Activity code

public class HomeActivity extends AppCompatActivity {

TextView tvName, tvEmail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ParseUser currentUser = ParseUser.getCurrentUser();

    tvName = findViewById(R.id.tvName);
    tvEmail = findViewById(R.id.tvEmail);

    if(currentUser!=null){
        tvName.setText(currentUser.getString("name"));
        tvEmail.setText(currentUser.getEmail());
    }
}

public void logout(View view) {
    ProgressDialog progress = new ProgressDialog(this);
    progress.setMessage("Loading ...");
    progress.show();
    ParseUser.logOut();
    Intent intent = new Intent(HomeActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
    progress.dismiss();
}

}

Here is an example of one of my switch case code

switch(item.getItemId()) {
                    case R.id.nav_home:
                        selectedfrag = new HomeActivity();
                        break;

The error codes says: Required Type Fragment

CodePudding user response:

easiest way for your case is to create a new fragment and move your code there

Or if you want to manually do it extend Fragment

override onCreateView

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_home, parent, false);
    }

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // add your activity's code here
    // ParseUser currentUser = ParseUser.getCurrentUser();.........
}
  • Related