Bertrand
Bertrand

Reputation: 251

Dismiss android activity displayed as Popup

So i have a service, that starts an activity displayed as a Popup thank's to "android:style/Theme.Dialog"

This Activity shows a Listview, with a list of application. On each element of the listview, there is a short description of the application, and two buttons. 1 for launching the application 2 for displaying a toast with more informations.

Here is the code in my service : it starts the activity

Intent intent = new Intent(this, PopUpActivity.class);
intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);  

this activity uses a custom layout, with a listview, adapted with a custom ArrayAdapter

In this adaptater, i've put an action on the start button in order to start the current application

Button lanceur = (Button) v.findViewById(R.id.Buttonlancer);
            lanceur.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    p.start(mcontext);
                }
            });

with p.start, i start the application.

But now, if i press "back" from the application, i go back to the popup... and i can start another application. I don't want it to be possible.

That's why i wish i could dismiss/destroy/finish my PopupActivity, but i can't manage to do it with the code i have.

Upvotes: 2

Views: 3987

Answers (3)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

This can be done with startActivityForResult() and onActivityResult()

 Intent intent = new Intent(this, PopUpActivity.class);
 intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 getApplicationContext().startActivityForResult(intent,1); 

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Log.d("CheckStartActivity","onActivityResult and resultCode = "+resultCode);
     // TODO Auto-generated method stub
     super.onActivityResult(requestCode, resultCode, data);
     this.finish();
 }

and add in AndroidManifest.xml:

<activity android:name=".PopUpActivity" android:noHistory="true" />

Upvotes: 3

FoamyGuy
FoamyGuy

Reputation: 46856

change your luanch code like this:

Button lanceur = (Button) v.findViewById(R.id.Buttonlancer);
lanceur.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        p.start(mcontext);
        finish();
    }
});

Also you probably shouldn't need to be calling getApplicationContext() in your Service can you paste some more code? You should be able to use NameOfYourService.this.startActivity(); or perhaps even just startActivity(); depending on how you have it structured.

Also note as per the Developer Docs it is a very bad idea for you to start an Activity directly from a service. see this question for reasons why: Why service should not start activity?

Upvotes: 0

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72331

Just call finish() inside your Activity that is displayed as pop-up, after calling start for your next Activity.

Upvotes: 0

Related Questions