使用上下文从自定义视图完成活动 [英] finish activity from custom view using context

查看:103
本文介绍了使用上下文从自定义视图完成活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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

当用户丢失游戏并想要重试时,会提示此代码。但是当我按重试超过4次时,应用程序崩溃。我怀疑内存泄漏。在logcat中进行了一些测试之后,我设法发现在重试后该活动仍然运行。



我的计划是用两件事来攻击这个问题。回收我的绘画和结束整体的第一个活动。但是,即使打完电话,第一个活动也没有关闭。任何帮助? (在我的代码的其他部分使用getContext()已经工作到目前为止)。



编辑:通过结束活动,它会自动破坏变量吗?或者我还需要清除Android内存中的位图?任何想法我该怎么做?

解决方案

如果 makeDialog2()活动中声明尝试使用 YourActivityName.this 而不是 getContext()。如果不是,那么尝试使用这个上下文传递给方法作为参数。 YourActivityName.this 从您调用该方法。


通过结束活动,它会自动消除变量吗?


完成活动应该销毁它和所有相关的资源。如果您稍后再启动 Activity 的新实例,则会重新创建其所有资源。除非您使用某种静态变量 - 只要您的应用程序正在运行,它们将保持活着。







这个方法被调用的地方实际上是surfaceview,所以我给它的任何参数将基本上仍然是getContext()。有没有办法解决?我不明白为什么((Activity)getContext())。finish();不工作


查看 c $ c> -class 的/app/Activity.html\">文档。



正如你所愿请参阅上下文 Activity 的超类 - 意思是每个活动是一个上下文,但不是每个上下文是一个活动 。换句话说((Activity)getContext())。finish(); 可能会导致一个 ClassCastException 。 >

您可以做些什么来验证您获得的上下文其实也是一个活动做一个这样的检查:

  if(getContext()instanceof Activity)
日志。 e(TAG,getContext()返回一个Activity!);

在你打电话之前添加它 finish()并检查LogCat是否同意其活动


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?

解决方案

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.

这篇关于使用上下文从自定义视图完成活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆