finish()无法关闭活动

时间:2014-05-04 10:49:55

标签: android

我在做什么 ::我正在尝试使用代码

关闭活动
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();
    }

}

{编辑}

Intent intent = new Intent(getActivity(), SplashActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent.putExtra("LOGOUT", true);
                startActivity(intent); 
    上面的
  • 片段来自splash.java
  • 旁边的第二个活动
  • 如何在启动时将布尔值启动为true SplashActivity.java
  • 我正在尝试的只是关闭我的应用程序&amp;回到android主页 屏

2 个答案:

答案 0 :(得分:0)

你是什么意思&#34;什么都不做&#34;?你期望发生什么?

finish()是异步的 - 它将请求传递给ActivityManager,后者将依次触发整个结束流程(完成堆栈中较新的活动 - &gt; onPause() - &gt; onStop() - &gt; onSaveInstance() .... - &gt; onFinish())。它不会突然杀死你的应用程序。

这是一件好事。

答案 1 :(得分:0)

尝试将getBoolean方法与Intent一起使用:

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

另外,启动{{1}时,请确保boolean的值不是&#34;为空&#34; (应该是true) }。如果这样可以解决问题,请告诉我。