如何让Action的后退按钮像Action Bar一样工作?

时间:2015-01-29 01:49:22

标签: android android-activity android-lifecycle

由于某种原因,按后退按钮会导致一些问题。但是,操作栏上的向后箭头(即向上导航)没有任何问题。我看到答案让按钮按下后退按钮,但我还没有看到任何使后退按钮像上按钮一样工作

提前致谢!

public class ListEventActivity extends ActionBarActivity {
JSONObject json = new JSONObject();
JSONArray jarr = new JSONArray();
JSONObject jobj = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_event);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    int year = extras.getInt("year");
    int month = extras.getInt("month");
    int day = extras.getInt("day");

    try {
        json.put("year", year);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        json.put("month", month);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        json.put("day", day);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    PostData pd = new PostData();
    pd.execute();

    try {
        pd.get(1000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }

    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    EventAdapter ea = new EventAdapter(createList(jarr.length()));
    recList.setAdapter(ea);
}

private List<EventInfo> createList(int size) {
    List<EventInfo> result = new ArrayList<EventInfo>();
    for (int i = 0; i < size; i++) {
        EventInfo ei = new EventInfo();
        try {
            jobj = jarr.getJSONObject(i);
            ei.id = Integer.parseInt(jobj.getString("id"));
            ei.title = jobj.getString("title");
            ei.startDatetime = jobj.getString("startDate");
            ei.pictureURL = jobj.getString("pictureURL");
            ei.type = jobj.getString("type");
            ei.contact = jobj.getString("contact");
            ei.endDatetime = jobj.getString("endDate");
            ei.location = jobj.getString("location");
            ei.description = jobj.getString("description");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        result.add(ei);
    }
    return result;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_list_event, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class PostData extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {
        ClientServerInterface clientServerInterface = new ClientServerInterface();
        jarr = clientServerInterface.postData("http://54.164.136.46/decodejson.php", json);
        return null;
    }
}
}

基本上,当用户点击日历上的日期时,会启动此活动。应用程序将年,月和日的JSON数据发送到服务器,服务器在数据库上运行查询,并返回事件详细信息的JSONArray。仅当JSONArray中有多个元素时才会出现此问题。当您单击返回时,它会将您带到ListEventActivity,只有事件,然后您必须再次单击返回以返回日历。使用操作栏上的向上箭头时不会发生这种情况。

2 个答案:

答案 0 :(得分:2)

这是你在找什么?

@Override
public void onBackPressed() {
        super.onBackPressed();
        pd.cancel(true);
        finish();
}

答案 1 :(得分:0)

override fun onBackPressed() {
      super.onBackPressed()
      NavUtils.navigateUpFromSameTask(this)
}

此代码在Kotlin中,您可以轻松地将其转换为Java。