手动回到previous活动 [英] Go back to previous activity manually

查看:123
本文介绍了手动回到previous活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:
活动1(主):如果DB有任何行检查。如果没有,加载主视图。如果它至少有1个起始活性2

Here's my code: Activity1 (main): Checks if the db has any rows. If not, load the main view. If it has at least 1 start Activity2

    int num = db.numOfRows();
    if(num==0){
        setContentView(R.layout.main);
    } else {
        startActivity(new Intent(this, Activity2.class));
    }

活动2:加载moreprojects查看哪些填充有DB信息表行

Activity 2: Loads the moreprojects view which populates table rows with db information.

        super.onCreate(savedInstanceState);
        setContentView(R.layout.moreprojects);
        populateRows();

非活动dbhandler:包含所有数据库的东西(在活动1分贝提到)。现在,活性2,你可以删除它调用下面的方法行。内,如果(NUM == 0)我想有活性2调用活动1。活动1是允许的项目创建画面。

non-activity dbhandler: contains all the database stuff (db mentioned in Activity1). Now, on Activity2 you can delete the rows which calls the method below. Within that if(num==0) I would like to have Activity2 call Activity1. Activity1 is the screen which allows for creating of projects.

public void deleteContact(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_blah, KEY_ID + " = ?",
            new String[] { String.valueOf(id) });
    int num = numOfRows();
    Log.d("Testing","Number of rows: "+num);

    if(num ==0){
        //go back to Activity1
    }
    db.close();
}

我希望这是有道理的。

I hope this makes sense.

我一直想多不同的事情,如:
创造一个活性2方法,它的确完成(); ...但是,这并不做任何事情。这似乎只是重装活性2。

I've been trying multiple different things such as: creating a method in activity2 which does finish();... but that doesn't do anything. It just seems to reload Activity2.

请帮帮忙!

推荐答案

调用<一个href=\"http://developer.android.com/reference/android/app/Activity.html#setResult%28int,%20android.content.Intent%29\"相对=nofollow>完成自打开使用活性2 应该回去startActivity不startActivityForResult:

Calling finish should go back since you opened activity2 using startActivity and not startActivityForResult :

public void deleteContact(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_blah, KEY_ID + " = ?",
            new String[] { String.valueOf(id) });
    int num = numOfRows();
    Log.d("Testing","Number of rows: "+num);

    if(num ==0){
        finish();
    }
    db.close();
}

这中假设deleteContact是活性2

this supposes that deleteContact is in Activity2

编辑:

调用完成将只调用活动1的onResume方法我猜结果
回到活动1,然后重新启动它,您可以:

calling finish will only call Activity1's onResume method i guess
To go back to Activity1 and restart it you can :

finish();
startActivity(new Intent(this, Activity1.class));

检查Android的活动的生命周期

这篇关于手动回到previous活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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