如何从静态方法中销毁Activity?

时间:2015-01-10 03:06:19

标签: java android

我的活动checkLoginTask中有一个子类LoginActivity(此活动用于登录用户)。从扩展AsynTask的类中的onPostExecute()调用此子类。

如果LoginActivty我要销毁活动theLoginOk == "ok"并开始活动MainActivity。我使用了finish(),但收到了错误Non-Static method "finish()" cannot be referenced from a static context

我尝试使用final Activity activity = this;,但没有效果。

这是我的Avtivity LoingPage

上的方法
public static void checkLoginTrue(JSONObject jsonObject, Context context){
    if(jsonObject != null) {
        Intent intent = new Intent(context, MainActivity.class);

        try {
            JSONObject student = jsonObject.getJSONObject("status");
            String theId = student.getString("id");
            String theLoginOk = student.getString("login");

            Log.i("JSON login", theLoginOk);

            if (theLoginOk.equals("ok")) {
                intent.putExtra("id", theId);
                intent.putExtra("login", theLoginOk);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else {
                // something
            }

        } catch (JSONException e) {
            Log.w("error", e.getMessage());
        }
    }
}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

使用广播接收器完成活动或事件总线,以避免内存泄漏,android中不应该存在单例!

示例:

public class FinishableActivity extends AppCompatActivity {
    public static final String ACTION_FINISH = FinishableActivity.class.getName() + ".FINISH";
    public static final String EXTRA_ACTIVITY_CLASS = "EXTRA_ACTIVITY_CLASS";

    private FinishBroadcastListener finishReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        finishReceiver = new FinishBroadcastListener()
                .register(this);
    }

    @Override
    protected void onDestroy() {
        finishReceiver.unregister(this);
        finishReceiver = null;

        super.onDestroy();
    }

    public static void show(Context context, Class<? extends Activity> cls) {
        context.startActivity(new Intent(context, cls) {{
            addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_REORDER_TO_FRONT | FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        }});
    }

    public static void hide(Context context, Class<? extends Activity> cls) {
        context.sendBroadcast(new Intent(ACTION_FINISH) {{
            putExtra(EXTRA_ACTIVITY_CLASS, cls);
        }});
    }

    class FinishBroadcastListener extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = (intent == null) ? "" : intent.getAction();
            action = (action == null) ? "" : action;

            if (ACTION_FINISH.contentEquals(action)) {
                onFinishCalled((Class<? extends Activity>) intent.getSerializableExtra(EXTRA_ACTIVITY_CLASS));
            }
        }

        public FinishBroadcastListener register(Context context) {
            context.registerReceiver(this, new IntentFilter(ACTION_FINISH));

            return this;
        }

        public void unregister(Context context) {
            context.unregisterReceiver(this);
        }
    }

    private void onFinishCalled(Class<? extends Activity> cls) {
        if (cls == null) {
            return;
        } else {
            if (cls.equals(getClass())) {
                finish();
            }
        }
    }
}

答案 1 :(得分:0)

这里不需要静态方法。如果你想练习在活动类中调用静态方法,请创建onr Util类,创建静态方法并从活动覆盖方法调用。 像

public class Utill
{

   public static void checkLoginTrue(JSONObject jsonObject, Context context, Class<? extends Activity> myClass){
        if(jsonObject != null) {
            Intent intent = new Intent(context, myClass);

            try {
                JSONObject student = jsonObject.getJSONObject("status");
                String theId = student.getString("id");
                String theLoginOk = student.getString("login");

                Log.i("JSON login", theLoginOk);

                if (theLoginOk.equals("ok")) {
                    intent.putExtra("id", theId);
                    intent.putExtra("login", theLoginOk);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                } else {
                    // something
                }

            } catch (JSONException e) {
                Log.w("error", e.getMessage());
            }
        }
    }
}

然后致电

 Utils.checkLoginTrue(jsonObject, this, MainActivity.class);
在您的活动中

任何覆盖非静态和/或静态方法。

答案 2 :(得分:0)

您尝试时finish()无效,因为您需要一个实际的活动对象来调用它。静态方法不在对象上运行。这就是为什么他们不能使用this

您必须将活动对象引用传递给静态方法,然后将finish()传递给它。如果您已将其作为Context context传递,则先将其转换为活动。例如:

//if your context is actually activity reference, use line below
//if not, add another Activity argument to the method
//if you cant get to the activity to pass it as argument, save it in a static reference somewhere, during ocCreate for example, and you can access it globally
Activity activity = (Activity) context;

//correct way to use finish()
activity.finish();