简体   繁体   中英

How can I return Home activity from child activity?

This is my code, I want to go to Home activity by clicking app icon button. My same code works fine for HomeActivity button .

package com.coderbd.englishtutor;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;


public class StudyActivity extends Activity implements OnClickListener{

Button app_icon_topBar;
ImageButton reset;
ImageButton settings;
ImageButton share;

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

    setContentView(R.layout.activity_study);
    app_icon_topBar=(Button) findViewById(R.id.app_icon_topBar);
    reset=(ImageButton) findViewById(R.id.btn_Reset);
    settings=(ImageButton) findViewById(R.id.btn_Settings);
    share=(ImageButton) findViewById(R.id.btn_Share);

    app_icon_topBar.setOnClickListener(this);
    reset.setOnClickListener(this);
    settings.setOnClickListener(this);
    share.setOnClickListener(this);
}

public void onClick(View v) {

    switch(v.getId()){
    case R.id.app_icon_topBar:
        finish();
        setContentView(R.layout.another_home_activity);
        break;
    case R.id.btn_Reset:
        finish();
        setContentView(R.layout.activity_contact);
        break;
    case R.id.btn_Settings:
        finish();
        setContentView(R.layout.activity_help);
        break;
    case R.id.btn_Share:
        finish();
        setContentView(R.layout.activity_scores);
        break;
    default:
        finish();
        setContentView(R.layout.another_home_activity);
        break;  
    }
}

Please help me.

Put this in your app icon button's onClick:

case R.id.app_icon_topBar:
    Intent intent = new Intent(StudyActivity.this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM