如何使 phone 的后退按钮像操作栏中的向上按钮一样工作?
How to make phone's back button work like up from Action Bar?
出于某种原因,按后退按钮会造成一些麻烦。但是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 年月日数据发送到服务器,服务器在数据库上运行查询,并且 return 是一个 JSON 事件详细信息数组。只有当 JSON 数组中有多个元素时才会出现此问题。发生的情况是,当您单击后退时,它会将您带到一个只有事件的 ListEventActivity,然后您必须再次单击后退以 return 到日历。使用操作栏上的向上箭头时不会发生这种情况。
这是您要找的吗?
@Override
public void onBackPressed() {
super.onBackPressed();
pd.cancel(true);
finish();
}
override fun onBackPressed() {
super.onBackPressed()
NavUtils.navigateUpFromSameTask(this)
}
此代码使用 Kotlin,您可以轻松将其转换为 Java。
出于某种原因,按后退按钮会造成一些麻烦。但是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 年月日数据发送到服务器,服务器在数据库上运行查询,并且 return 是一个 JSON 事件详细信息数组。只有当 JSON 数组中有多个元素时才会出现此问题。发生的情况是,当您单击后退时,它会将您带到一个只有事件的 ListEventActivity,然后您必须再次单击后退以 return 到日历。使用操作栏上的向上箭头时不会发生这种情况。
这是您要找的吗?
@Override
public void onBackPressed() {
super.onBackPressed();
pd.cancel(true);
finish();
}
override fun onBackPressed() {
super.onBackPressed()
NavUtils.navigateUpFromSameTask(this)
}
此代码使用 Kotlin,您可以轻松将其转换为 Java。