Save state when returning to an activity

0

I have a registration activity with 4 fields: 1 editText and 3 Spinners. At the click of a button the data filled in these fields are registered in the database and lead to another activity with an imageview of confirmation, which when passing 2 seconds returns to the activity of registration automatically.

In this case, I need it to inherit the fill that was before the click of the button, so the user does not have to type everything again and change only what he needs. How can I do this? Here's the click I need to call confirmation activity with imageView:

btn_Poliform.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            produto.setMatricula(Integer.parseInt(editText_matricula.getText().toString()));
            produto.setSupervisao(spinner_supervisao.getSelectedItem().toString());
            produto.setMaterial(spinner_material.getSelectedItem().toString());
            produto.setQuantidade(Integer.parseInt(spinner_quantidade.getSelectedItem().toString()));

            if(btn_Poliform.getText().toString().equals("REGISTRAR AGORA")){

                bdHelper.salvarProduto(produto);
                bdHelper.close();
            }

        }
    });

Here is the confirmation activity with the time of 2 seconds:

public class Finalizando extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_finalizando);

    final int MILISEGUNDOS = 2000;
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            Intent intent = new Intent(Finalizando.this, RegistrosMateriais.class);
            Finalizando.this.startActivity(intent);
        }
    }, MILISEGUNDOS);
}
    
asked by anonymous 11.03.2018 / 03:34

3 answers

0

Instead of re-launching the Activity RecordsMaterials, in the run() method, make finish() .

public class Finalizando extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_finalizando);

    final int MILISEGUNDOS = 2000;
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            finish();
        }
    }, MILISEGUNDOS);
}
    
11.03.2018 / 20:31
1

You can solve this problem in several ways. I would do it using startActivityForResult . When you give% commit_of% of the commit activity, you pass a pro bundle containing all the completed information.

In the registration activity, when you start the confirmation activity, you pass the pre-recorded data.

Bundle bundle = new Bundle();
bundle.putString("editText1", stringEditText1);
bundle.putBoolean("spinner1", isSpinner1Selected)
bundle.putBoolean("spinner2", isSpinner2Selected)
bundle.putBoolean("spinner3", isSpinner3Selected)
Intent intent = new Intent(this, ConfirmActivity.class);
intent.putExtras(bundle);
startActivityForResult(intent, 1);

At the end of the confirmation activity, you end up passing the bundle you received

Intent intent = new Intent();
intent.putExtras(getIntent().getExtras());
setResult(Activity.RESULT_OK, intent);
finish();

And in the registration activity you implement the callback with the returned data in the confirmation activity

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

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            Bundle bundle = data.getExtras();
            String editTextString = bundle.getString("editText1");
            boolean isSpinner1Selected = bundle.getBoolean("spinner1");
            boolean isSpinner2Selected = bundle.getBoolean("spinner2");
            boolean isSpinner3Selected = bundle.getBoolean("spinner3");
        }
    }
}

And with these variables you can fill in the data again.

A full example of the Android documentation on how to use the startActivityForResult: link

    
11.03.2018 / 06:09
0

Make termination msg with dialog and more viable than doing a new activity, follow code

public class AgradecimentoDialog extends DialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //aqui faz seu dialog ser em tela cheia
        setStyle(STYLE_NO_FRAME,android.R.style.Theme_Holo_Light);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        //Cria seu image view
        ImageView imageView = new ImageView(getActivity());


//        imageView.setImageResource(recource); basta adicionar sua imagem de agradecimento
        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(param);

        return imageView;
    }
}

Then just show the dialog that in the activity itself and end the msm bet save

 AgradecimentoDialog dialog = new AgradecimentoDialog();
    dialog.show(getFragmentManager(),"dialog");// mostra seu dialog na tela
    final int MILISEGUNDOS = 2000;
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            finish();
        }
    }, MILISEGUNDOS);
    
11.03.2018 / 20:24