L
L
lesia432021-12-04 14:45:10
android studio
lesia43, 2021-12-04 14:45:10

How to change Activity code to Fragment code?

Hello, I recently started using Android Studio, I wrote a program like notes in an Activity, but it happened that I need to rewrite this Activity under Fragment. I was looking for a solution on different sites, but I didn’t really understand how, if someone knows how to do it, then the water code of the Activity itself, I really ask you to help

import android.os.Bundle;
 
import com.arhiser.todolist.R;
import com.arhiser.todolist.model.Note;
import com.arhiser.todolist.screens.details.NoteDetailsActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
 
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
 
    private RecyclerView recyclerView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
 
        recyclerView = findViewById(R.id.list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
 
        final Adapter adapter = new Adapter();
        recyclerView.setAdapter(adapter);
 
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NoteDetailsActivity.start(MainActivity.this, null);
            }
        });
 
        MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        mainViewModel.getNoteLiveData().observe(this, new Observer<List<Note>>() {
            @Override
            public void onChanged(List<Note> notes) {
                adapter.setItems(notes);
            }
        });
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2021-12-05
@lesia43

onCreate will move to onCreateView
findViewById() will have to be called from the view, which you will get from the light through the inflater.
instead of this where the content of the form is required, you will use getActivity () or requireActivity ()
In general, create an empty form with a fragment through the wizard and slowly drag it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question