Android: Crouton + Activities state

297 Views Asked by At

I'm using Crouton im my project.In app i have few activities. In sub activities i want do some operations after finishing a process i make redirect to main activity and display Crouton message about result in previous activity. But hier i have problems...Crouton closes quickly after redirect. I found some solution:

1) Create intent for sub activity 2) close main activity 3) start sub activity

do some work...

4) Create intent for main activity 5) Put info in extra about changes 5) open main activity

and in main activity, in onCreate, i trying get extra about changes. I tried get info from extra in onStart, onResume..but still the same..Crouton message closes quickly, faster than needed.

What i am doing wrong? Maybe my algorithm with displaying Crouton is wrong..Pls correct me.

Thx a lot!

2

There are 2 best solutions below

0
On BEST ANSWER

I found solution to my question:

@Override
    protected void onResume() {
        super.onResume();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                displayEvents();
            }
        }, 500);

    }

private void displayEvents(){

        //Проверяю какое уведомление необходимо показать
        if(intent.hasExtra("event")){

            if(intent.hasExtra("event") && intent.getStringExtra("eventType").equals("confirm")) {

                Crouton.makeText(MainActivity.this, intent.getStringExtra("event"), Style.CONFIRM).show();
                getIntent().removeExtra("event");
                getIntent().removeExtra("eventType");

            }

        }

    }

done!

1
On

You have two options:

1) You can open your second activity with startActivityForResult() and than handle the result in your MainActivity to show the crouton. In the Android docs you can find a good tutorial for it: Android developer After ending the second activity you handle the result code and show the right text.

2) You can send it as you mentioned in a Extra with startActivity() and pass the in the intent: start activity

Hope it helps!