Convert Activity to Fragments

Asked

Viewed 438 times

2

Hello, I’m in need of a lot of help, I’m developing an application for my TCC, I’ve assembled most of the features and now that I’ve been messing with the visual part I’ve had problems, I mounted everything on activitys, but now I wanted to use the Tabslayouts, who use Fragments as I understand it, I’ve assembled some examples of Tabslayouts with Fragments, but I can’t carry the functions I had created in Activitys for these Fragments, anyone know how to do that?? Thanks in advance for the help !!!

Example of Activity

public class morador extends AppCompatActivity {

private TextView txt_exibe_nome, txt_exibe_idade, txt_exibe_apto,txt_exibe_bloco;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_morador);
//instanciando onde vai exibir os dados
    txt_exibe_nome = (TextView) findViewById(R.id.txt_exibe_nome);
    txt_exibe_idade = (TextView) findViewById(R.id.txt_exibe_idade);
    txt_exibe_apto = (TextView) findViewById(R.id.txt_exibe_apto);
    txt_exibe_bloco = (TextView) findViewById(R.id.txt_exibe_bloco);

    if(SharedPrefManager.getInstance(this).isLoggedIn()){
        finish();
        startActivity(new Intent(this, login.class));
    }

    txt_exibe_nome.setText(SharedPrefManager.getInstance(this).exibeMoradorNome());
    txt_exibe_idade.setText(SharedPrefManager.getInstance(this).exibeMoradoridade());
    txt_exibe_apto.setText(SharedPrefManager.getInstance(this).exibeMoradorApto());
    txt_exibe_bloco.setText(SharedPrefManager.getInstance(this).exibeMoradorBloco());
  }
}

Fragment where I want to play that code

public class Fragment_perfil extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_perfil, container, false);
 }

}

Fragmentadapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] mTab;
public MyFragmentPagerAdapter(FragmentManager fm, String[] mTab) {
    super(fm);
    this.mTab = mTab;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new Fragment_perfil();
        case 1:
            return new Fragment_perfil();
        default:
            return null;
    }
}

@Override
public int getCount() {
    return this.mTab.length;
}

@Override
public CharSequence getPageTitle(int position) {
    return this.mTab[position];
 }
}

1 answer

1

Your Fragment should look something like this

public class Fragment_perfil extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_perfil, container, false);
    // Copiar código do OnCreate após o setContentView
    // Trocar os findViewById por rootView.findViewById
    // Trocar this por getActivity()
    return rootView;
 }

}

NOTE: I am assuming that you will use in Fragment the same layout of Activity.

  • This code will return from the Fragment the data to the correct Activity ???

  • I did not understand the question. Return what?

  • I thank you for your reply, @Márciooliveira! His explanation was more than enough to understand the situation and solve the problem and could have been accepted quietly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.