Why does the back tubbon call onDestroy() on the parent?
I have the following scenario:
Activity A that opens Activity B via an intent
Intent intent = new Intent(parent.getContext(), activityB.class);
intent.putExtra(STATE_REST, gson.toJson(myObject));
startActivity(intent);
When I click on back on activity B (and only then) activity A fires onDestroy() followed by onCreate().
Manifest:
Activity A
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
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 B
<activity
android:name=".activities.MenuActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:label="#string/menu_title"
android:parentActivityName=".activities.MainActivity">
</activity>
your problem seems to be resolved in the flowing link:
Why is onDestroy always called when returning to parent activity?
Good luck.
Related
when my app starts it says to Login if you don't login you can register
in the register screen if you press back btn you don't turn back to login but the app is closing. any ideas?
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="#style/Theme.Design.Light.NoActionBar">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegisterActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".Setting"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".History"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".Fall_test"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".Examples"
android:screenOrientation="portrait">
</activity>
</application>
When you start register activity like
Intent mIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(mIntent);
this.finish(); OR LoginActivity.this.finish();
Just remove the below one if you used it
this.finish(); OR LoginActivity.this.finish();
and just start RegisterActivity like this
Intent mIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(mIntent);
When you press back button inside RegisterActivity it will get you back to LoginActivity.
You are finishing / destroying your activity when you're moving to another activity. When you go from one actity to another, the old activity is reserved in a stack. And when you press back button, it's popped back from the stack. So, if you finish the old one while moving to the new one, the activity won't be reserved in the stack, thus you are being forced to exit the app as there's no activity on the stack.
I'm using this code to refresh MainActivity.java when a Refresh button is pressed.
Intent intent = getIntent();
finish();
startActivity(intent);
MainActivity.java has the category default and there is another Launcher activity. So whenever I press the Refresh button the Launcher Activity also starts again. I only need to start the MainActivity class. Below is the manifest
<application
android:allowBackup="true"
android:icon="#drawable/play_icon"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme" >
<activity
android:name="com.theanilpaudel.joshilo.FirstScreen"
android:configChanges="orientation|screenSize"
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="com.theanilpaudel.joshilo.MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What you are looking for in this case is an explicit Intent that launches MainActivity.
You can do this like so:
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
This will ensure that the Intent only launches MainActivity.
However, I highly recommend re-evaluating why you need to completely restart the Activity to refresh it. There are much cheaper (faster and less resource intensive) methods of updating data displayed in an Activity.
I guess I know the culprit. Try using the following code once.
Intent intent = getIntent();
startActivity(intent);
finish();
As far as I know startActivity() uses context to run. In case if you call finish() before startActivity() that may lead to destroy context of MainActivity and will get the application's context i.e. fires the activity with behavior LAUNCHER or MAIN.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Let me know if it helps or you still have difficulty reloading activity.
Cheers
I have two activities in my application. SplashActivity and MainActivity. Here is their manifest definition.
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I want to finish SplashActivity after it shown , as you see it is defined as noHistor and calling MainActivity after 10 seconds so ;
Intent a = new Intent(this,MainActivity.class);
a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
finish();
When i take application to background and trying to resume , it is not resuming from MainActivity , it shows SplashActivity again.. how can i prevent this ? is it a known bug ?
Thank you very much
Activity A start muzic player(B) with intent, going back from music player shows the homescreen not activty A.
here is the acitvity in manifest
<activity android:name="myactivity"
android:launchMode="singleTop"
android:noHistory="true"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and Intent to start muzic player
Intent i = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/"+"rec1.wav");
i.setDataAndType(uri, "audio/*");
startActivity(i);
Expected results: Going back from muzicplayer, activity A shall be visible as per activity life cycle.
Hi the problem is your android:noHistory="true"
please remove this from your XML.
<activity android:name="myactivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
hope this might be help you.
I have 3 activities, then I navigate from A to B and from B to C. The stack are C-B-A (with C on the top). The C Activity have a Runnable that works every interval, and connect to a web service that update the UI. If the Activity C comes to background, is destroyed and then comes to foreground again the task starts with de main Activity A. I want to come back to the C activity. Is possible?
In the emulator works fine but in my smartphone with only this application running, always destory the entire application.. can I do somthing? If I do a service separated from the UI, could improve this?
Edit2: I try this declaration on AndroidManifest
<activity android:name=".ActivityA" android:label="#string/app_name"
android:alwaysRetainTaskState="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="oauthht" android:host="hattrick"/>
</intent-filter>
</activity>
<activity android:name=".ActivityB" android:label="#string/ActivityB"
android:screenOrientation="portrait"></activity>
<activity android:name=".ActivityC" android:label="#string/ActivityC"
android:screenOrientation="portrait"
android:launchMode="singleTask">
Edit2: And the code to call the activities:
In the main Activity A:
public void onClickButtonB(View button) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(this,ActivityB.class));
startActivity(intent);
}
In the Activity B:
Bundle bundle = new Bundle();
bundle.putLong("selectedMatch", matchID);
Intent liveIntent = new Intent();
liveIntent.setComponent(new ComponentName(ActivityB.this, ActivityC.class));
liveIntent.putExtras(bundle);
startActivity(liveIntent);
I know which was the problem. The application crashes in background but the exception was caught. This restarts the app in background and mislead me. :-S