androidup-button

Using activity.finish in up button


I want to define an up button for my android application to go to parent activity. From some resources I found this code when up-button clicked:

NavUtils.navigateUpFromSameTask(getActivity());

But I had a problem and it was that my activity needs some data for creation (Intent extras) and for this reason I replaced with this code:

getActivity().finish();

But some feeling tells me this is not a correct way for reaching to parent activity and maybe I will face to some runtime errors later. What do you think what should I do?


Solution

  •   Intent myIntent = new Intent(sourceActivity.this,targetActivity.class);
      myIntent.putExtra("NameKey", yourValue); // for send a Value with a key name. yourValue can be every type value
      startActivity(myIntent);
      this.finish();
      // and for receive value from target activity
      Bundle extraGet = getIntent().getExtras();
      String receiveValue = extraGet.getString("NameKey");
    

    also you can define public static your variable that can use it to every class and activities

    i think you want this:

     Intent intent = NavUtils.getParentActivityIntent(this);
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     NavUtils.navigateUpTo(this, intent);