Kartik Prabhu
Kartik Prabhu

Reputation: 411

How to make phone's back button work like up from Action Bar?

For some reason, pressing the back button causes some trouble. However, the back arrow on the action bar, which is the up navigation, does not have any problems. I saw answers to make the up button work like the back button, but I haven't seen any that make the back button work like the up button.

Thanks in advance!

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

So basically, this activity is started up when a user clicks on a date on a calendar. The app sends JSON data of the year, month, and date to the server, and the server runs a query on a database, and returns a JSONArray of event details. This problem is only occurring when there is more than one element in the JSONArray. What happens is, when you click back, it takes you to a ListEventActivity, with only event, and then you have to click back once again to return to the calendar. This does not happen when the up arrow on the action bar is used.

Upvotes: 2

Views: 912

Answers (2)

Javad
Javad

Reputation: 147

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

This code is in Kotlin you can easily convert it to Java.

Upvotes: 0

Eugene H
Eugene H

Reputation: 3568

Is this what you are looking for?

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

Upvotes: 2

Related Questions