当我按下操作栏上的后退按钮时如何进入不同的页面?

时间:2016-05-27 16:10:11

标签: android android-fragments android-actionbar back-button

我正在尝试转到ProfileFragment页面但是当我尝试按下操作栏上的后退按钮时,它会转到LoginActivity,这是另一个不相关的活动。我怎么能这样做?

这是代码: 我的设置活动.java

class db {
 private static $_instance = null;
    private $_pdo,
            $_query,
            $_error = false,
            $_result,
            $_lastinserted = false,
            $_count = 0;

    private function __construct() {
        try {
            $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
        } catch(PDOException $e) {
            die($e->getMessage());
        }
    }

    public static function getInstance() {
        if(!isset(self::$_instance)) {
            self::$_instance = new db();
        }

        return self::$_instance;
    }

    public function query($sql, $params = array()) {
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)) {
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }
            if($this->_query->execute()){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
                $this->_lastinserted = $this->_pdo->lastInsertId();
            } else {
                $this->_error = true;
            }
        }

        return $this;
    }

    public function results() {
        return $this->_results;
    }

    public function count() {
        return $this->_count;
    }
}

2 个答案:

答案 0 :(得分:0)

您无法创建指向Intent的{​​{1}}。从Fragment开始,您应该创建SettingsActivity以转到托管Intent的{​​{1}}。

而不是写

CombineFragmentActivity

你应该写

ProfileFragment

Intent intent = new Intent(getApplicationContext(), ProfileFragment.class); 函数中。

答案 1 :(得分:0)

我相信你在谈论向上导航。在这种情况下,您可以为当前活动声明父活动,它将始终带您到那里。

来自Android文档:

https://developer.android.com/training/implementing-navigation/ancestral.html

<activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>