gpt4 book ai didi

android - 如何使手机的后退按钮像操作栏中的向上按钮一样工作?

转载 作者:行者123 更新时间:2023-11-29 00:13:42 25 4
gpt4 key购买 nike

出于某种原因,按下后退按钮会引起一些麻烦。但是action bar上的后退箭头,也就是向上导航,没有任何问题。我看到了使向上按钮像后退按钮一样工作的答案,但我还没有看到任何使后退按钮像向上按钮一样工作的答案。

提前致谢!

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;
}
}
}

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

最佳答案

这是您要找的吗?

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

关于android - 如何使手机的后退按钮像操作栏中的向上按钮一样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com