2017-06-03 3 views
0

私はアンドロイドアプリケーションを開発中で、現在のアクティビティから以前のアクティビティまでナビゲートしたいと考えています。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); 



     } 


    } 

} 

activity_second.xml

<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> 
+0

はあなたのMainActivityを終了していますあなたのナビゲーションアイコンのクリックでonBackPressed()を呼び出しますSecondActivity? –

答えて

0

その非常に単純な、あなたが開いたときに

toolbar1.setNavigationIcon(R.drawable.back); 
    toolbar1.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 
関連する問題