Android Back Navigation ParentActivityName not working - android

i tried using this tutorial on how to provide a back navigation as in this code
https://developer.android.com/training/implementing-navigation/temporal.html
<activity android:name=".controller.general.AccountPickerScreen"
android:parentActivityName="ph.edu.upm.agila.extendthelife.controller.general.LogInScreen">
<intent-filter>
<action android:name="ph.edu.upm.agila.extendthelife.controller.general.AccountPickerScreen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I already specificied the parentActivityName but whenever I press the back button the app exits? any solutions?
tried this
public void onBackPressed() {
Intent intent = NavUtils.getParentActivityIntent(AccountPickerScreen.this);
startActivity(intent);
finish();
}
it does work however i want to know if it is the right way of doing it?
what does finish() do exactly

You may need to override the onBack for the activity and use NavUtils to accomplish what you want with a line like:
Intent intent = NavUtils.getParentActivityIntent(AccountPickerScreen.this);

for api level below 16 try adding
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="fully qualified path to Activity Class Name" />
in activity tag in androidmanifest.xml

<activity android:name=".controller.general.AccountPickerScreen">
<intent-filter>
<action android:name="ph.edu.upm.agila.extendthelife.controller.general.AccountPickerScreen" />
<category android:name="android.intent.category.DEFAULT" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="ph.edu.upm.agila.extendthelife.controller.general.LogInScreen" />
</intent-filter>
</activity>
override onBackPressed with `
NavUtils.navigateUpFromSameTask(this);`

I had an issue where the parent button worked on some activities but not others.
The problem was my onOptionsItemSelected function. It was returning true by default, which blocked the call from completing. You should call super.onOptionsItemSelected(item).
See https://developer.android.com/guide/topics/ui/menus.html

Related

Unable to fully close activity when using "singleTask" as launch mode

I'm trying to create a separate activity and use it to receive data from other applications. This SecondaryActivity is being launched through intent.action.SEND and android.intent.action.SEND_MULTIPLE in the manifest. I have also set it to use launchMode="singleTask" to avoid a duplicate app problem (if you keep selecting the app in the android share sheet and clicking back you can spin up multiple instances of the app).
launchMode="singleTask seems to work so far but I cannot get it to close even when adding finish() and finishAffinity() in onBackPressed(). When i look at the back stack, it says that SecondaryActivity is closed, but upon pressing the navigaton button, I can still see the activity.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.TestProject">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondaryActivity"
android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
</application>
My SecondActivity looks like this
class SecondaryActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_secondary)
}
override fun onBackPressed() {
finish()
finishAffinity()
}
}
This problem happens when the app is completely closed and is launched through sharing a file. I cannot get the SecondaryActivity to not show up in the navigation screen even after closing it by pressing the back button. adb says there are no activities in the stack but i still see it in the navigation.
This is called Recents screen which is a system-level UI that lists recently accessed activities and tasks even if the application has finished.
If you don't want to see the app in the recent apps after pressing back button, using finishAndRemoveTask
override fun onBackPressed() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAndRemoveTask()
} else {
finish()
}
}

How to make an activity above the MainActivity.java?

My current MainActivity is a navigation drawer.
I want to make another activity on top of the navigation drawer.
Lets make that activity StartActivity.
On StartActivity there is a start button.
What i want to do is make the StartActivity opens up first when the app runs.
And when the user presses the START button on the StartActivity, it will direct him/her to the navigation drawer.
is this possible?
Create the StartActivity.
somewhere call "startActivity(intentforMainactivity);
go to the manifest and move
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
from the
<activity
android:name="sehtestapp.MainActivity"
into
<activity
android:name="sehtestapp.StartActivity"
Your Application will then start the StartActivity first
1) Create another Activity, that you'll call StartActivity
2) Set up a layout that you'll use in your StartActivity with a
button in it.
3)Create the onClickListener to launch a new Intent when clicked.
Make it launch the MainActivity.
4) Change your AndroidManifest as follow :
<activity
android:name="XXX.StartActivity"
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="XXX.MainActivity"
android:parentActivityName="XXX.StartActivity" />
You can try changing the Launcher activity and send intent having a boolean bundled in it when START button is pressed, in MainActivity fetch the intent, read that boolean value and open the drawer programmatically.

unable to startActivity master-detail from another activity

I've an activity WelcomeActivity.java in which theres a button bContinue. In the OnClick method of the button, I tried this..
startActivity(new Intent(this, MenuItemDetailActivity.class));
//startActivity(new Intent("com.resto.demo.activity.MENUITEMLISTACTIVITY"));
neither of the above 2 lines work. instead they give me NullPointerException & the program ends abruptly..I cant find the problem.. is the problem in manifest or my call?
<activity
android:name="com.resto.demo.activity.MenuItemListActivity"
android:label="#string/title_menuitem_list" >
<intent-filter>
<action android:name="com.resto.demo.activity.MENUITEMLISTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
MenuItemListActivity is the part of master/detail flow.
Thanx you. Also tell me if my problem is not understood. Any help/suggestion is welcome :)
Edit your manifest
<application
<activity
android:name="com.resto.demo.activity.MenuItemListActivity"
android:label="#string/title_menuitem_list" >
<intent-filter>
<action android:name="com.resto.demo.activity.MENUITEMLISTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.resto.demo.activity.MenuItemDetailActivity"/>
</application>
And add this to OnClick event
startActivity(new Intent(MenuItemListActivity.this, MenuItemDetailActivity.class));
The first thing I would do is make sure MenuItemDetailActivity is in the AndroidManifest as well (instead of just MenuItemListActivity)
<activity
android:name="com.resto.demo.activity.MenuItemDetailActivity"
android:label="..." >
</activity>
However, this does not normally throw a NullPointerException. Can you post the error message/stack trace for the NullPointerException? Also, it would be great to post the lines of code that the NullPointerException references.

Open only one activity, without the main activity

I have an application that has one service and 2 activities.One activity (the main one) is a preferences activity and the other one is a dialog themed activity. The service from time to time needs to open only the dialog themed activity (using FLAG_ACTIVITY_NEW_TASK).
The problem is that when the preferences activity is opened in background (for example the user pressed HOME key instead of BACK, so the activity is OnPause) and the service tries to open the dialog themed activity, also the main activity is opened (comes in foreground).
I don't want this. So, how can I open only the themed activity from the service, without the main activity pop up ?
Android Manifest .xml
<service android:name=".SimpleService"> </service>
<activity
android:name=".Preferences" 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=".popup_activity" android:theme="#android:style/Theme.Dialog" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="screenon.popup.activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And from the service :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory("screenon.popup.activity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I believe you're looking for the android:launchMode attribute.
Alex , I believe that all you want is to display preferences activity just once & then it must die(or lets say finish()) .
In that case you can override onResume of the preferences activity
to finish your preferences activity after it is resumed after pause
#Override
public void onResume()
{
//if activity is resumed after onPause then only run it
this.finish(); //simply kill your activity if it is resumed after pause
}
EDIT-To know whether the activity was paused you need to override onPause().I think you can dig out the rest.
Hope it helps!

Two searchable.xml activities in one AndroidManifest.xml

I have an Android app which has a few different activities for browsing articles and images downloaded from RSS.
I'd like to be able to offer to hook up the search button to the Search dialog, using the a searchable.xml file. I've managed to do this with one search, using:
<activity android:name=".search.SearchResultsActivity"
android:label="#string/search_results_activity_title" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable_articles"/>
</activity>
and in the <application />
<meta-data android:name="android.app.default_searchable"
android:value=".search.SearchResultsActivity" />
I can now launch the Search dialog from any activity, and it launches the SearchResultsActivity.
I would now like to be able to search for images when the user is an ImageListActivity, using a searchable_images.xml, and use the default everywhere else.
I have a SearchResultsImageActivity which includes the following meta-data element, and used the same element in the ImageListActivity.
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable_images"/>
On pressing the search button in the ImageListActivity, I get the default search from searchable_articles.xml.
If I change the default_searchable to SearchResultsImageActivity, the image search is always launched, and the article search is never launched.
If I remove the default_searchable meta-data element altogether, and add searchable meta-data only selected activities, no search is launched.
I'm fairly sure this should be possible, but I don't know what I'm doing wrong.
In your Manifest file update the ImageListActivity activity tag
<activity
android:name=".ImageListActivity"
...
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsImageActivity" />
</activity>
So when you will trigger native search in ImageListActivity it will invoke the SearchResultsImageActivity and default one for others.
Assuming SearchResultsImageActivity is searchable.
One way I did this was to create fake activities then switch out the activities when you need them.
<activity android:name="activitySearchMain" />
<activity android:name="activitySearchSub1">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:scheme="user" />
</intent-filter>
</activity>
<activity android:name="activitySearchSub2">
<intent-filter>
<action android:name="com.sample.twitter.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:scheme="user" />
</intent-filter>
</activity>
Create two class that are named for the sub activities.
then create intents like this when component is clicked...
Intent sourceIntent = getIntent();
Intent newIntent = new Intent(this, activitySearchSub2.class);
newIntent.setAction(activitySearchSub2.ACTION2);
newIntent.setData(sourceIntent.getData());
startActivity(newIntent);
finish();
and call the intents from onClick when a button is clicked or some other component is click:
If you override the search function in just that activity, it should prevent the search call from going up to the application level. The return value controls if the call is propagated up.
#Override
public boolean onSearchRequested() {
onPromptSearch();
return false; // don't go ahead and show the search box
}

Categories

Resources