Issue
I have an Activity that has a ListView as the main Activity. When one of the items in the list is selected, it starts a new activity displaying detailed information about the item from the list that was selected. The code runs fine, and the relevant details are as follows:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("test","test");
ListView listView=(ListView) findViewById(R.id.name_list);
ListAdapter adapter=new SimpleAdapter(this,contactList,
R.layout.list_item,new String[]{TAGS[2]},new int[]{R.id.list_name_display});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent intent=new Intent(MainActivity.this,SingleContactActivity.class);
intent.putExtra(EXTRA_CONTACT_POSITION,position);
startActivity(intent);
}
});
}
The newly created activity has the main activity as it's parent, so when I am done looking at it, I press the up button on the action bar to return to the list view. However, the Log.d(...) line tells me that every time I go to the detailed information activity, and then return to the main activity, the main activity is recreated.
I don't want this and the way that I read this:https://developer.android.com/training/basics/activity-lifecycle/stopping.html is that the main activity should be stopped when the new one starts, and then started and then resumed on the up button press. Is anyone aware of the reason that my main activity is destroyed?
Thanks for the help
EDIT: Manifest File
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SingleContactActivity"
android:label="@string/title_activity_single_contact"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.jefe.jefecontacts.MainActivity" />
</activity>
</application>
Solution
hmm.. what you could do is this, show the information not inside a new activity, but use a dialog instead. This way your activity remains active, so it won't get recreated.
if you want it fullscreen you can use: MyDialog = new Dialog(mContext, android.R.style.Theme_NoTitleBar_Fullscreen);
use your own layout: MyDialog.setContentView(R.layout.myLayout);
Add Buttons (like a back button): final Button myBackButton = (Button)MyDialog.findViewById(R.id.my_back_button);
And use them:
myBackButton.setOnClickListenernew View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialog.dismiss();
}
});
or override keys:
MyDialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) { //Back key pressed
MyDialog.dismiss();
}
return true;
}
});
Hope this will help you out ;)
Answered By - Strider
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.