Android finish()在关闭活动时不起作用

Android finish()在关闭活动时不起作用,android,Android,我正在执行的操作:我正在尝试使用代码关闭活动 if (getIntent().getBooleanExtra("LOGOUT", false)) { SplashActivity.this.finish(); } 发生了什么事 我的控制权到了那条线(我正在传递上一条线的意图) 活动)但完成不起作用 为什么会这样 我怎样才能克服这个问题 SplashActivity.java public class SplashActivity extends FragmentActivity

我正在执行的操作:我正在尝试使用代码关闭活动

if (getIntent().getBooleanExtra("LOGOUT", false))
{
      SplashActivity.this.finish();
}
发生了什么事

  • 我的控制权到了那条线(我正在传递上一条线的意图) 活动)但完成不起作用
  • 为什么会这样
  • 我怎样才能克服这个问题
SplashActivity.java

public class SplashActivity extends FragmentActivity {
    /** Called when the activity is first created. */


    JSONObject jsonobject;  
    JSONArray jsonarray;
    private String Content;
    DatabaseAdapter databaseHelper;
    TextView txtSplashTitle,txtSplashDesc;
    Cursor cursor;
    LocationManager locationManager;
    LocationListener locationListener;
    public static String srcLatitude;
    public static String srcLongitude;
    public static String destLatitude;
    public static String destLongitude;
    public static String buffetOfferId;
    private String tag_json_obj = "jobj_req";
    private ProgressDialog pDialog;

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

        if (getIntent().getBooleanExtra("LOGOUT", false))
        {
            SplashActivity.this.finish();
        }

        //Remove title bar
        this.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //set content view AFTER ABOVE sequence (to avoid crash)
        setContentView(R.layout.splash);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            showDialog();
        }
        //Delete database if it exists
        SplashActivity.this.deleteDatabase(FindMyBuffetConstants.DATABASE_NAME);
        databaseHelper = new DatabaseAdapter(this);


        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);

        JsonObjReqWithVolly();
    }

    private void JsonObjReqWithVolly() {
        showProgressDialog();
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                Const.URL_JSON_OBJECT, null,
                new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d("VollyResponse", response.toString());
                parseBuffetTableJSON(response);
                distanceCalculation();
                parseBuffetDetailsTableJSON(response);
                hideProgressDialog();
                startingNewActivity();
            }

            private void parseBuffetTableJSON(JSONObject response) {
                //jsonobject = new JSONObject(response);
                try {
                    jsonobject = response.getJSONObject("findmybuffet");
                    jsonarray = jsonobject.getJSONArray("buffets");
                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        map.put("buf_off_id", jsonobject.getString("buf_off_id"));
                        map.put("from_time", jsonobject.getString("from_time"));
                        map.put("to_time", jsonobject.getString("to_time"));
                        map.put("online_price", jsonobject.getString("online_price"));
                        map.put("reserved_price", jsonobject.getString("reserved_price"));
                        map.put("buf_image", jsonobject.getString("buf_image"));
                        map.put("res_name", jsonobject.getString("res_name"));
                        map.put("rating", jsonobject.getString("rating"));
                        map.put("latitude", jsonobject.getString("latitude"));
                        map.put("longitude", jsonobject.getString("longitude"));
                        map.put("buf_type_name", jsonobject.getString("buf_type_name"));
                        map.put("from_date", jsonobject.getString("from_date"));
                        map.put("to_date", jsonobject.getString("to_date"));
                        map.put("city_id", jsonobject.getString("city_id"));
                        map.put("city_name", jsonobject.getString("city_name"));
                        map.put("meal_type_id", jsonobject.getString("meal_type_id"));
                        map.put("meal_type_name", jsonobject.getString("meal_type_name"));
                        map.put("buf_desc", jsonobject.getString("buf_desc"));
                        map.put("distance", jsonobject.getString("distance"));
                        //Log.d("----$$$----", map.toString());
                        //Calling database 
                        databaseHelper.addDataToBuffetTable(map);
                    }   
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            private void parseBuffetDetailsTableJSON(JSONObject response) {
                //jsonobject = new JSONObject(response);
                try {
                    jsonobject = response.getJSONObject("findmybuffet");
                    jsonarray = jsonobject.getJSONArray("buf_meal_det");
                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        map.put("buf_off_id", jsonobject.getString("buf_off_id"));
                        map.put("menu_type_id", jsonobject.getString("menu_type_id"));
                        map.put("menu_type_name", jsonobject.getString("menu_type_name"));
                        map.put("cuisine_type_name", jsonobject.getString("cuisine_type_name"));
                        map.put("item_name", jsonobject.getString("item_name"));
                        //Log.d("----$$$----", map.toString());
                        //Calling database 
                        databaseHelper.addDataToBuffetDetailsTable(map);
                    }   
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            private void distanceCalculation() {
                String myQuery="SELECT * FROM buffets";
                Cursor cursor = (Cursor) databaseHelper.getAllDataFrmBuffetTable(myQuery);
                cursor.moveToFirst();
                if(cursor.moveToFirst()){
                    do{
                        buffetOfferId=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))); 
                        destLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(9)));
                        destLongitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(10)));
                        srcLatitude="12.918286";
                        srcLongitude="77.669493";
                        Log.d("---Source------", destLatitude+","+destLongitude);
                        Log.d("---Destination-", srcLatitude+","+srcLongitude);
                        float CalculateDistance=CalculateDistance(srcLatitude,srcLongitude,destLatitude,destLongitude);
                        Log.d("---Result---", CalculateDistance+"");
                        //round float to nearest whole number and then convert into double and converting to kilometers
                        double CalculateDistanceRounded = Math.round(CalculateDistance)/1000;
                        databaseHelper.updateSqLiteDistance(CalculateDistanceRounded,buffetOfferId);
                    }while(cursor.moveToNext());
                }
            }

            private float CalculateDistance(String srcLatitude,
                    String srcLongitude, String destLatitude, String destLongitude) {

                Location locationA = new Location("point A");
                //Convert from string to double and then process
                locationA.setLatitude(Double.parseDouble(destLatitude));
                locationA.setLongitude(Double.parseDouble(destLongitude));
                Location locationB = new Location("point B");
                locationB.setLatitude(Double.parseDouble(srcLatitude));
                locationB.setLongitude(Double.parseDouble(srcLongitude));
                return locationA.distanceTo(locationB);
            }

            private void startingNewActivity() {
                Intent intent=new Intent(SplashActivity.this,MainActivity.class);
                startActivity(intent);
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("VollyResponse", "Error: " + error.getMessage());
                hideProgressDialog();
            }
        }) {
        };
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);
        // Cancelling request
        // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);       
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        //Setting fonts for textviews
        checkForGpsAndTurnItOn();
    }

    public void showDialog(){
        //GPS-Dialog
        GpsEnablingDialog gpsAlert=new GpsEnablingDialog();
        gpsAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
    }

    private void checkForGpsAndTurnItOn() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            Log.d("My-Log-Msg","$-before-showDialog-$");
            showGpsDialog();
            Log.d("My-Log-Msg","$-after-showDialog-$");
        }
    }

    public void showGpsDialog(){
        //GPS-Dialog
        GpsEnablingDialog gpsAlert;
        try {
            gpsAlert = new GpsEnablingDialog();
            gpsAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("My-Log-Msg",e.toString());
            e.printStackTrace();
        }
    }

    private void showProgressDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideProgressDialog() {
        if (pDialog.isShowing())
            pDialog.hide();
    }

}
  • 上面的代码片段来自splash.java旁边的第二个活动
  • 如何在启动时将布尔值作为true启动 SplashActivity.java
  • 我只是想关闭我的应用程序,返回安卓主页 屏风
    • 你说的“什么都不做”是什么意思?你预计会发生什么

      finish()。它不会突然杀死你的应用程序


      这是一件好事。

      尝试使用
      getBoolean
      方法,使用
      Intent
      如下:

      if ( getIntent().getExtras().getBoolean("LOGOUT") ) {
          SplashActivity.this.finish();
      } else {
          Log.v("Boolean LOGOUT","The value is false...");
      }  
      

      另外,在启动
      SplashActivity
      时,请确保
      布尔值不为“空”(应为
      true
      )。让我知道这是否解决了问题。

      没有必要编写
      SplashActivity.this.finish()如果您的代码正在
      SplashActivity
      上下文中运行。只需执行
      finish()
      ;如果你想在返回SplashActivity@Sky时完成你的应用程序,你应该调用
      startActivity(intent)下面的另一个
      finish()
      来自您在编辑中共享的代码段。例如:
      getActivity().finish()finish()
      。这应该行得通。谢谢你的帮助!。。。。我跟随这篇文章也是为了实现我的目标:)你的问题清楚地表明你的“控制”达到了那一条线。现在看来你的问题不同了。下一次请尽量做到准确。而且要说的是-getExtras()将解析意图的额外内容。
      if ( getIntent().getExtras().getBoolean("LOGOUT") ) {
          SplashActivity.this.finish();
      } else {
          Log.v("Boolean LOGOUT","The value is false...");
      }