Antonis sinotnA Antonis sinotnA - 18 days ago 15
Android Question

Go back from activity to fragment with Up button

In main activity i've created two fragments used as tabbed menu. One of the fragments contains a list where list items starts a new activity when clicked. I wanna go back from new activity to the fragment (list) using the Up Action/Navigation button but no list is displayed, only an empty tab. I have declared parent activity as the main activity in manifest.xml.

When i use phone's back button it works fine.

List is created in fragment's onViewCreated method.

Manifest :


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:parentActivityName=".MainActivity" >
</activity>
</application>




Fragment with listView which start the new activity :

public static class frag extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

public frag() {
}
public static frag newInstance(int sectionNumber) {
frag fragment = new rangingFrag();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);


return rootView;
}

@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
ListView listView = (ListView) rootView.findViewById(R.id.mobile_list);
final ArrayAdapter adapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.listview);
listView.setAdapter(adapter);
adapter.addAll(beacons);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String selectedText = (String) parent.getItemAtPosition(position);
Bundle bundle = new Bundle();
bundle.putString("param1", selectedText);
Intent myIntent = new Intent(v.getContext(), Main2Activity.class);
myIntent.putExtras(bundle);
startActivity(myIntent);
}
}
);

} }

Answer
in Your Activity where you want to keep back arrow on Action Bar , on Create write
ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    and override 

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            super.onOptionsItemSelected(item);
            switch (item.getItemId()) {
                case android.R.id.home:
                    finish();
                    break;
                      }

            return true;
        }
Comments