Android 在安卓系统中,当点击后退图标而不是完整的工具栏时,如何导航到上一个活动?

Android 在安卓系统中,当点击后退图标而不是完整的工具栏时,如何导航到上一个活动?,android,toolbar,back,Android,Toolbar,Back,我正在开发一个android应用程序,希望从当前活动导航到上一个活动 在这里,我想从SecondActivity导航回MainActivity。我添加了一个工具栏和一个后退图标,当单击时,应该可以导航回上一个活动。但在这里,单击时,完整的工具栏将导航回上一个活动 SecondActivity.java package com.example.acer.videoapp; import android.app.ProgressDialog; import android.content.Inte

我正在开发一个android应用程序,希望从当前活动导航到上一个活动

在这里,我想从SecondActivity导航回MainActivity。我添加了一个工具栏和一个后退图标,当单击时,应该可以导航回上一个活动。但在这里,单击时,完整的工具栏将导航回上一个活动

SecondActivity.java

package com.example.acer.videoapp;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;


public class SecondActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();
    private ProgressDialog pDialog;
    private ListView listView1;
    Toolbar toolbar1;
    String subjectName;

    ArrayList<HashMap<String, String>> lessonList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //setting title to toolbar
        toolbar1 = (Toolbar) findViewById(R.id.toolbar1);

        Bundle bundle = getIntent().getExtras();
        if(bundle!=null) {
            toolbar1.setTitle(bundle.getString("Subject"));
            subjectName=toolbar1.getTitle().toString();
        }

        lessonList = new ArrayList<>();
        listView1 = (ListView) findViewById(R.id.list);
        new GetLessons().execute();

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                HashMap<String, String> lesson = lessonList.get(i);
                intent.putExtra("number", lesson.get("number"));
                intent.putExtra("name", lesson.get("name"));
                //intent.putExtra("subject", listView1.getItemAtPosition(i).toString());
                startActivity(intent);
            }
        });

        //back navigation
        toolbar1.setNavigationIcon(R.drawable.back);
        toolbar1.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),MainActivity.class));
            }
        });

    }

    /* Async task class to get json by making HTTP call */
    private class GetLessons extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(SecondActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String url = getResources().getString(R.string.lessons_url, subjectName);
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    // Getting JSON Array
                    JSONArray lessons = new JSONArray(jsonStr);

                    // looping through All lessons
                    for (int i = 0; i < lessons.length(); i++) {
                        JSONObject c = lessons.getJSONObject(i);

                        String number = c.getString("lessonNo");
                        String name = c.getString("lessonName");

                        // tmp hash map for single lesson
                        HashMap<String, String> lesson = new HashMap<>();

                        // adding each child node to HashMap key => value
                        lesson.put("number", number);
                        lesson.put("name", name);


                        // adding lesson to lesson list
                        lessonList.add(lesson);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Json parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),"Couldn't get json from server. Check LogCat for possible errors!",Toast.LENGTH_LONG).show();
                    }
                });

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    SecondActivity.this, lessonList,R.layout.list_item, new String[]{"number", "name",}, new int[]{R.id.lnumber,R.id.lname});
            listView1.setAdapter(adapter);



        }


    }

}
package com.example.acer.videoapp;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类SecondActivity扩展了AppCompatActivity{
私有字符串标记=MainActivity.class.getSimpleName();
私人对话;
私有列表视图列表视图1;
工具栏工具栏1;
字符串subjectName;
ArrayList lessonList;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_秒);
//将标题设置为工具栏
toolbar1=(Toolbar)findViewById(R.id.toolbar1);
Bundle Bundle=getIntent().getExtras();
if(bundle!=null){
工具栏1.setTitle(bundle.getString(“主题”));
subjectName=toolbar1.getTitle().toString();
}
lessonList=新建ArrayList();
listView1=(ListView)findViewById(R.id.list);
新建GetLessons().execute();
listView1.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父项、视图、int i、long id){
意向意向=新意向(SecondActivity.this,ThirdActivity.class);
HashMap lesson=lessonList.get(i);
intent.putExtra(“数字”),lesson.get(“数字”);
intent.putExtra(“名称”,lesson.get(“名称”);
//intent.putExtra(“主题”,listView1.getItemAtPosition(i.toString());
星触觉(意向);
}
});
//反向导航
工具栏1.setNavigationIcon(R.drawable.back);
toolbar1.setNavigationOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startActivity(新意图(getApplicationContext(),MainActivity.class));
}
});
}
/*异步任务类通过HTTP调用获取json*/
私有类GetLessons扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=newprogressdialog(SecondActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串url=getResources().getString(R.String.lessons\u url,subjectName);
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
//获取JSON数组
JSONArray课程=新JSONArray(jsonStr);
//循环学习所有课程
对于(int i=0;ivalue
第一课。把(“数字”,数字);
第一课。把(“名字”,名字);
//将课程添加到课程列表
增加(课);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“Json解析错误:”+e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“无法从服务器获取json。请检查LogCat是否存在可能的错误!”,Toast.LENGTH\u LONG.show();
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
SecondActivity.this,lessonList,R.layout.list_项,新字符串[]{“number”,“name”,},n
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.acer.videoapp.SecondActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/blue"
        android.theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/toolbar1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />

</RelativeLayout>
    toolbar1.setNavigationIcon(R.drawable.back);
    toolbar1.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });