Back button does not work after displaying a contact

I play with the "Clickable List Items" code from Pro Android 4. The code is basically:

public class MainActivity extends ListActivity implements OnItemClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); CursorLoader loader = new CursorLoader(this, Contacts.CONTENT_URI, null, null, null, Contacts.DISPLAY_NAME + " ASC"); Cursor cursor = loader.loadInBackground(); String[] columns = new String[] { Contacts.DISPLAY_NAME }; int[] views = new int[] { android.R.id.text1 }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); setListAdapter(adapter); getListView().setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, id); Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); startActivity(intent); } } 

All of this works great. I see the contact list, and when I click the name, it opens the address book for this person.

The problem is that the back button does not work from the address book. According to the book, clicking the "Back" button from the address book should return me to the above Activities. But instead, I returned to the main screen.

My application still works when I switch to it manually, I return to it, and the list view is in the correct previous scroll position, etc.

This is on Android 4.1.1. I am wondering if there is anything extra that I have to implement in order to make this work?

This is the manifest file:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.contactslist" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.READ_CONTACTS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
+6
source share
1 answer

It looks like your activity stack has messed up somehow. Try adding the following before you start your startActivity call (intent)

 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
0
source

Source: https://habr.com/ru/post/921518/


All Articles