Save state when returning to an Activity

Asked

Viewed 842 times

0

I have an Activity of registration with 4 fields: 1 editText and 3 Spinners. At the click of a button the data entered in these fields are recorded in the database and lead to another Activity with a confirmation imageView, which when passing 2 seconds back to the registration Activity automatically.

In this case, I need it to come back by inheriting the fill that was before the click of the button, so that the user does not need to type everything again and change only what he needs. How can I do that? 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);
}
  • Because you have made another Activity only to show the finishing screen, and you could do a Dialogfragment in the same acitivity and still control everything locally in Activity, it makes no sense to go out creating Activity without having a specific activity for it

  • You’re right but I don’t know how to do a Dialogfragment, I didn’t even know there was KKK, can you help me? I need a very large confirmation for the user to understand that it was really saved, understand why I chose an imageView separates soon..

  • I will post the answer you need to make a dialog full screen that looks like an Activity

3 answers

1

You can solve this problem in many ways. I would do using the startActivityForResult. When you give the startActivity from Activity confirmation, you pass a Bundle pro Intent containing all the filled information.

In Registration Activity, when you start the confirmation Activity, you pass the 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 Registration Activity you implement callback with the data returned 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 replenish the data.

A complete example of Android documentation for using startActivityForResult: https://developer.android.com/training/basics/intents/result.html?hl=pt-br

  • Where do I put the id of my fields?

  • You do not need to save the id of your fields, only the value that is in them. onActivityResult run you arrow the values back into them.

  • But these values I do not know, the user can type/select anything. (I put the code). D

0

Make msg finishing with dialog and more feasible than making a new Activity, follows 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 own Activity and finish the save bet msm

 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);

0


Instead of re-launching the Activity Registrosmaterials, in the method run(), do 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);
}
  • Thanks! It worked.. And in case I wanted to delete 1 field only?

  • What do you mean "delete 1 field only"?

  • Return to Activity of registration inheriting 3 instead of 4 fields.

  • If you want to "clear" any of the fields already filled do so before calling Activity Finishing.

Browser other questions tagged

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