完成()不关闭工作活动 [英] finish() not working in closing the activity

查看:144
本文介绍了完成()不关闭工作活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么 ::我试图用code关闭活动

What i am doing :: i am trying to close the activity using code

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

这是怎么回事


  • 我的控制涉及到该行(我传递的意图从previous
    活动),但完成不工作

  • 为什么会出现这种情况?

  • 如何克服这个问题?

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

  • 如何启动布尔值,真当我启动
    SplashActivity.java

  • 所有我试图只是关闭我的应用程序和放大器;返回Android主
    屏幕

  • 推荐答案

    尝试使用 getBoolean 方法与意图如:

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

    此外,请您布尔的确定值不是的的(应该是真实)当您启动 SplashActivity 。让我知道如果问题解决了。

    Also, make sure the value of your boolean is not "empty" (it should be true) when you launch SplashActivity. Let me know if this resolve the issue.

    这篇关于完成()不关闭工作活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆