Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finish activity from custom view using context

Here's my code:

    private void makeDialog2() {
    AlertDialog.Builder about = new AlertDialog.Builder(getContext());
    about.setTitle("You Won!");

    about.setPositiveButton("Play Again",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent playIntent2 = new Intent(getContext(),
                            PracticePlayActivity.class);
                    playIntent2.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    getContext().startActivity(playIntent2);
                    ((Activity) getContext()).finish();
                }
            });

    about.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg1, int arg2) {
            Intent playIntent = new Intent(getContext(),
                    PlayChooserActivity.class);
            playIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            getContext().startActivity(playIntent);
            ((Activity) getContext()).finish();
        }
    });

    about.show();
}

This code is prompted when the user loses the game and wants to retry. However when i press retry more than 4 times the application crashes. I'm suspecting a memory leak. After some testing in logcat i managed to find out that the activity is still running after retrying.

My plan is to attack this problem with two things. Recycling my drawables and ending the first activity as a whole. However, the first activity is not closing even after i call finish. Any help? (Using getContext() in other parts of my code has worked so far).

EDIT: By ending the activity does it destroy the variables automatically? or do i still need to clear the bitmaps from the Android memory? Any ideas how i can do this?

like image 517
user1012016 Avatar asked Dec 21 '11 08:12

user1012016


People also ask

How do you end an activity in context?

If you have access to the running view of the Activity you want to finish (for example, you are in a click listener), you could do: ((Activity)getContext()). finish();

How can I get active activity in Android?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view to show activity name.

What is a custom View Android?

Custom Views is just a way to make an android developer a painter. When you need to create some custom and reuse the views when it is not provided by the Android Ecosystem. Custom Views can be used as widgets like TextView, EditText etc.


2 Answers

If makeDialog2() is declared inside an Activity try using this or YourActivityName.this instead of getContext(). If it's not, then try passing the Context to the method as a parameter using this or YourActivityName.this from where you call the method.

By ending the activity does it destroy the variables automatically?

Finishing an Activity should destroy it and all related resources. If you start a new instance of the Activity later on it will re-create all its resources. Unless you use some kind of static variables - they will stay "alive" as long as your app is running.


The place where this method is being called from is actually the surfaceview therefore any parameter i give it will essentially still be getContext(). Is there any way around this? I don't see why ((Activity) getContext()).finish(); isn't working

Take a look at the documentation for the Activity-class.

As you can see Context is a superclass of Activity - meaning that every Activity is a Context but not every Context is an Activity. In other words ((Activity) getContext()).finish(); might cause a ClassCastException.

What you could do to verify that the Context you get is in fact also an Activity is do a check like this:

if( getContext() instanceof Activity )
   Log.e( "TAG", "getContext() returns an Activity!" );

Add that right before the you call finish() and check if LogCat agrees that its an Activity.

like image 105
kaspermoerch Avatar answered Oct 31 '22 12:10

kaspermoerch


I solved it. Perhaps it is a bug but i had to first call the class name before the getContext() function. Perhaps its a bug with the dialog function or an error in my code structure.

ClassName.this.getContext()
like image 20
user870380 Avatar answered Oct 31 '22 10:10

user870380