当我返回事件时触发事件

时间:2017-06-10 11:21:01

标签: android android-activity android-button

当我回到按下后退按钮的活动时,我试图触发一个事件。

我想要做的是当我回到后面按钮重新加载一些项目。有没有办法做到这一点?

这是我的主要活动,我想做"重新加载"数据,有些像" onResume"或" onReEnter"



package com.example.juanfri.seguridadmainactivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ActivityTemporadaJson extends AppCompatActivity {



    private String Nombre;
    private int IdTmdb;
    private String Tipo;
    private int NumeroTemp;
    private DBHelper mydb;
    private ProgressDialog pDialog;
    public final String API = "5e2780b2117b40f9e4dfb96572a7bc4d";
    public final String URLFOTO ="https://image.tmdb.org/t/p/original";
    private Temporada temp;
    private TextView nombrePelicula;
    private ImageView fotoPortada;
    private TextView sinopsis;
    private TextView NumEpsVal;
    private TextView fechaLanzVal;
    private ArrayList<Episodio> episodios;
    private RecyclerView recyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private RecyclerAdapterEpisodio mAdapterEp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temporada_json);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Intent recep = this.getIntent();
        episodios = new ArrayList<>();
        Nombre = recep.getStringExtra("Nombre");
        Tipo = recep.getStringExtra("Tipo");
        IdTmdb = Integer.parseInt(recep.getStringExtra("idSerie"));
        NumeroTemp = Integer.parseInt(recep.getStringExtra("NumTemp"));
        this.setTitle(Nombre);
        nombrePelicula = (TextView) this.findViewById(R.id.NombrePelicula);
        fotoPortada = (ImageView) this.findViewById(R.id.FotoPortada);
        sinopsis = (TextView) this.findViewById(R.id.Sinopsis);
        NumEpsVal = (TextView) this.findViewById(R.id.NumEpsVal);
        fechaLanzVal = (TextView) this.findViewById(R.id.FechaLanzVal);
        recyclerView = (RecyclerView) this.findViewById(R.id.recyclerviewEpisodios);
        mydb = new DBHelper(this);
        if (Tipo.equalsIgnoreCase("SQL")) {
            Cursor res = mydb.getResultQuery("SELECT count(e.Visto) as numVisto FROM Episodio e, Temporada t WHERE t.IdTMDB = " + IdTmdb + " and e.IdTemporada = t.IdTemporada and e.NumeroTemporada = " + NumeroTemp + " and e.visto = 1");
            res.moveToFirst();
            int NumVisto = res.getInt(0);
            mydb.UpdateEpsVistos(NumVisto, IdTmdb, NumeroTemp);
            TextView NumEpsVis = (TextView) this.findViewById(R.id.EpsVis);
            TextView NumEpsVisVal = (TextView) this.findViewById(R.id.EpsVisVal);
            NumEpsVis.setVisibility(View.VISIBLE);
            NumEpsVisVal.setVisibility(View.VISIBLE);
            AQuery androidAQuery = new AQuery(this);
            res = mydb.getResultQuery("SELECT Nombre, Sinopsis, FechaInicio, Poster, NumeroEpisodios,EpisodiosVistos FROM Temporada WHERE IdTMDB = " + IdTmdb + " and NumeroTemporada = " + NumeroTemp);
            res.moveToFirst();

            nombrePelicula.setText(res.getString(0));
            sinopsis.setText(res.getString(1));
            fechaLanzVal.setText(res.getString(2));
            androidAQuery.id(fotoPortada).image(res.getString(3), true, true, 150, 0);
            NumEpsVal.setText(Integer.toString(res.getInt(4)));
            NumEpsVisVal.setText(Integer.toString(res.getInt(5)));

            Cursor resEps = mydb.getResultQuery("SELECT e.Nombre, e.NumeroEpisodio, e.NumeroTemporada, e.FechaEmision, e.Sinopsis, e.Poster, e.Visto, e.IdEpisodio FROM Episodio e, Temporada t WHERE t.IdTMDB = " + IdTmdb + " and e.IdTemporada = t.IdTemporada and e.NumeroTemporada = " + NumeroTemp);
            resEps.moveToFirst();
            while (resEps.isAfterLast() == false) {
                boolean visto = false;
                if (resEps.getInt(6) == 1) {
                    visto = true;
                }
                Episodio nuevo = new Episodio(resEps.getInt(7), 0, resEps.getString(0), resEps.getInt(1), resEps.getInt(2), resEps.getString(3), resEps.getString(4), resEps.getString(5), visto);
                episodios.add(nuevo);
                resEps.moveToNext();
            }
            mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(mLinearLayoutManager);
            mAdapterEp = new RecyclerAdapterEpisodio(episodios, IdTmdb, Tipo);
            recyclerView.setAdapter(mAdapterEp);

        } else {
            new GetTemp(this).execute();
        }

        //mydb = new DBHelper(this);
    }

    private class GetTemp extends AsyncTask<Void, Void, Void> {
        Context c;
        public GetTemp(Context c)
        {
            this.c = c;
        }



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

            // Making a request to url and getting response
            //url = "https://api.themoviedb.org/3/tv/airing_today?api_key="+API+"&language=en-US&page="+pagina;
            //https://api.themoviedb.org/3/tv/57243/season/1?api_key=5e2780b2117b40f9e4dfb96572a7bc4d&language=en-US
            String url = "https://api.themoviedb.org/3/tv/"+IdTmdb+"/season/"+NumeroTemp+"?api_key="+API+"&language=es-ES";
            String jsonStr = sh.makeServiceCall(url);


            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray episodes = jsonObj.getJSONArray("episodes");
                    String fecha = jsonObj.getString("air_date");
                    String nombreSerie = jsonObj.getString("name");
                    int numEps = episodes.length();
                    int numTemp = jsonObj.getInt("season_number");
                    String sinop = jsonObj.getString("overview");
                    String poster =URLFOTO + jsonObj.getString("poster_path");

                    // looping through All Contacts
                    for (int i = 0; i < episodes.length(); i++) {
                        JSONObject c = episodes.getJSONObject(i);
                        Episodio nuevo = new Episodio();
                        nuevo.setSinopsis(c.getString("overview"));
                        nuevo.setFechaEmision(c.getString("air_date"));
                        nuevo.setNombreEpisodio(c.getString("name"));
                        nuevo.setNumeroEpisodio(c.getInt("episode_number"));
                        nuevo.setNumeroTemporada(c.getInt("season_number"));
                        nuevo.setPoster(URLFOTO + c.getString("still_path"));
                        episodios.add(nuevo);

                    }
                    temp = new Temporada(0, IdTmdb, nombreSerie, sinop, fecha, poster, numTemp,false, 0, numEps, episodios);

                } catch (final JSONException e) {
                    Toast.makeText(getApplicationContext(),
                            "Json parsing error: " + e.getMessage(),
                            Toast.LENGTH_LONG)
                            .show();
                }
            } else {
                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);
            AQuery androidAQuery=new AQuery(c);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
            {
                pDialog.dismiss();
            }
            androidAQuery.id(fotoPortada).image(temp.getPoster(), true, true, 150,0);
            String Nombre = "Temporada " + temp.getNumeroTemporada();
            nombrePelicula.setText(Nombre);
            sinopsis.setText(temp.getSinopsis());
            NumEpsVal.setText(Integer.toString(temp.getNumeroEpisodios()));
            fechaLanzVal.setText(temp.getFechaInicio());


            mLinearLayoutManager = new LinearLayoutManager(c, LinearLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(mLinearLayoutManager);
            mAdapterEp = new RecyclerAdapterEpisodio(temp.getEpisodios(),IdTmdb,Tipo);
            recyclerView.setAdapter(mAdapterEp);


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

        }

    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以使用onRestart,这将在现有活动被重新购买时触发。 正如文档中所引用

  

在重新显示当前活动时,在onStop()之后调用   给用户(用户已导航回用户)。它将被遵循   通过onStart()然后onResume()。

所以覆盖onRestart

@Override
public void onRestart() {
    // you come back to me 
}