can not return to parent activity from child activity opened by menu in Android App - android-activity

manifest:
<activity
android:name=".SettingsActivity"
android:parentActivityName="com.boropost.app.MainActivity" />
<activity
android:name=".FindActivity"
android:parentActivityName="com.boropost.app.MainActivity" />
menu:
<item
android:id="#+id/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_find"
app:showAsAction="always"/>
In my case, the child SettingsActivity is opened via a menu item that is not showed on the action bar. Clicking the back button does not return to the parent MainActivity. The onActivityCreated in MainActivity is not triggered neither, however the child FindActivity that is opened via a menu item that is showed on the action bar does not have this issue.

Please add below lines on onCrate() method
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
And Add below method in your child SettingsActivity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}

Related

.NET Maui pushing a new content page from a Toolbar Item

I've got a toolbar item in the navigationbar of a Maui app. This is a shell app. I've got a toolbar item in the nav bar. When I touch the Add toolbar item, the click event happens, but nothing happens in the app. I don't see my CreateUser() page. Any suggestions in how to view the page are appreciated.
My content page:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ScanAndScoringMauiApp.Pages.UserSearch"
Title="UserSearch">
<Shell.TitleView>
<StackLayout>
<Button Text="ADD" x:Name="Add" Clicked="Add_Clicked" HeightRequest="50" WidthRequest="100" HorizontalOptions="End"></Button>
</StackLayout>
</Shell.TitleView>
My Add_Clicked event is various iterations of the code below. I've tried iterations of this without using InvokeOnMainThreadAsync and on and on.
private async void Add_Clicked(object sender, EventArgs e)
{
await MainThread.InvokeOnMainThreadAsync(() =>
{
Navigation.PushAsync(new CreateUser());
});
}

How to disable a Dynamic Menu Contribution parent in Eclipse 4 RCP Application

This question stems from
How to disable or enable a MMenu (not MMenuItem) in an Eclipse E4 application
I have been attempting to grey-out/disable an entire Dynamic Menu Contribution in Eclipse 4 when a condition is met in the application. The Dynamic Menu Contribution is itself in the File Menu Model Element. My workaround has been to remove all options so the menu does not show anything, but is still active (not-grey) when the condition is met with the code below for clearing the menu.
items.clear();
if (checkMenuEnabled()) {
Fillthemenu();
}
This code below doesn't seem to disable the dynamic menu contribution like I want it to.
MenuImpl menu = (MenuImpl) modelService.find("menuID", application.getChildren().get(0).getMainMenu());
menu.setEnabled(checkMenuEnabled());
Here is an image of the model xmi UI items. The File->Submenu is what I am trying to grey out. Not the individual Dynamic Menu Contribution Items.
Model XMI
Thanks
So in your e4xmi file, you have a "Menu" with a "Dynamic Menu Contribution" and you want to gray out some items in the menu on some application condition, right?
The "Dynamic Menu Contribution" is attached to some "class", right?
In this class, when you generate a disabled "menu":
public class <the class referenced in e4xml> {
#Inject private EModelService modelService;
#AboutToShow
public void aboutToShow(List<MMenuElement> items, {...}) {
MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class);
dynamicItem.setLabel(<some label>);
dynamicItem.setIconURI(<some icon URI>);
dynamicItem.setContributorURI("platform:/plugin/platform:/plugin/<nom plugin>");
dynamicItem.setContributionURI(<menu item handler> "bundleclass://<plugin name>/<menu item handler class>");
--> dynamicItem.setEnabled(true/false); to enable/grey out the menu
--> dynamicItem.setvisible(true/false); to show/hide the menu
// add one or many MDirectMenuItems ...
items.add(dynamicItem);
}
}
In the menu item handler ("setContributionURI" class) where you implement the logic of the menu item, you can also show/hide/enable/disable the menu item:
public class <menu item handler class> {
#Execute
public void execute({...}) {
<code linked to the menu item selection here>
}
#CanExecute
public boolean canExecute(#Optional MMenuItem menuItem, {...}) {
// implement the logic to show/hide, enable/disable the menu item
menuItem.setVisible(true/false); // show/hide the menu item
return true/false; // enable/grey out the menu item
}
}

Parent Acitivity's status not kept when backing from child activity

In AndroidManifext.xml, I have :
<activity
android:name=".mypackage.ChildActivity"
android:label="Child"
android:parentActivityName=".mypackage.ParentActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".view.activities.ParentActivity" />
</activity>
Inside ParentActivity, I have a few fragements, A, B, C. At fragment C, I launch ChildActivity when clicking a button. When backing to ParentActivity, I am not landing on Fragment C, but Fragment A. It appears that ParentActivity is relaunched or initialized again.
In ChildActivity, I have:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
As a comparison, I also tried to replace ChildActivity with a Fragment D and backing from fragment D correctly lands on fragment C.
Where did I miss for the ChildActivity implementation?
Edited:
Just noticed that when clicking "Back" button on device, it properly backs to Fragment C of ParentActivity, but if hitting the "<" button at the left top corner in Child Activity, that's where issue arises.
Thanks in advance!
Shawn
Remove the mentioning parent from your childActivity. Like below. Because android already maintains the stack of navigations
<activity
android:name=".mypackage.ChildActivity"
android:label="Child"
android:parentActivityName=".mypackage.ParentActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize">
</activity>

Actionbar up navigation with fragment and activity

I'm going to use home button (Action bar App icon) as back button. I got it to work but not in the way i intended.
My MainActivity is an activity which holds (1) a drawer that shows a list of categories. And a Fragment that displays a list of items in the category chosen in the drawer.
when a item in the list is clicked, a new DetailActivity is started to show the details.
here starts the problem:
From the DetailActivity when i press Back button, it returns to the MainActivity as it was before clicking the item to show details. That is what I expect. However, when use home button as Up navigation, it starts the MainActivity as if I opened the app again. Not showing the list that was previously being shown.
I read in developer documents that for fragments I have to use: .add(detailFragment, "detail") And .addToBackStack() then commit.
But what am I suppose to add in add(---,"---"). And then how should I use it?!
this is my codes:
the method is the MainActivity that shows the content:
public void refreshDisplay(Context context, View view, String category, int i) {
List<Lesson> lessonByCategory = datasource.findByCategory(category, i);
final ListView lv = (ListView) view.findViewById(R.id.listView);
final ArrayAdapter<Lesson> adapter = new LessonListAdapter(context, lessonByCategory);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
Log.i(LOGTAG, "onListItemClick called");
ArrayAdapter<Lesson> m_adapter = adapter;
// get the Lesson object for the clicked row
Lesson lesson = m_adapter.getItem(position);
Intent intent = new Intent(MainActivity.this, LessonDetailActivity.class);
intent.putExtra(".model.Lesson", lesson);
intent.putExtra("isStared", isStared);
startActivityForResult(intent, LESSON_DETAIL_ACTIVITY);
}
});
}
In my LESSON_DETAIL_ACTIVITY that shows the detail content I have this code to enable up navigation for home button:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// I have some other cases here
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
And finally in the Manifest I used the code below to introduce MainActivity as the parrent of LessonDetailActivity:
<activity
android:name=".LessonDetailActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
I want the Home button as up navigation to behave like back button so that when its clicked it takes me to the MainActivity as it was before opening the LessonDetailActivity. The code above doesn't do that and every time I press Home in the action bar it starts the MainActivity from scratch.
Could anyone help me with this please?
I also should say that I'm new so I'd appreciate it if the answers were detailed.

Any example of changing menus and supporting closing for an 'AllActive' tabbed application?

I'm a CM newbie and trying to get my head around CM. I am building an application where each tab allows the user different functionality for accessing a server where the active work in all tabs (if any is active) could be running async to each other so the shell is an "Conductor.Collection.AllActive" which I hope was the correct choice. I am looking for suggestions or an sample in two areas -
I would like to have the main shell own the application menu and the tab control, and to change the application menuitems depending on the tab selected and then have the menuitem clicks routed to the respective VM for the tab.
Since all tabs could potentially be doing active work simultaneously, I am hoping for an example of how the VM on each tab can participate in helping to decide (via dialogs to the user) if the application can be closed if the close menuitem or the application X icon is clicked. Or if the close should be cancelled per the user response (e.g. they say 'no' since there are unsaved files).
Any examples and suggestions much appreciated.
I created a sample of a possible way to do this. Using a SharedViewModel that contains a collection of MenuItems. The SharedViewModel is injected into the ShellViewModel and each TabViewModel. The Menu control binds to the collection of MenuItems.
When a tab's OnActivate fires the Menu items can be updated by the TabViewModel.
<HierarchicalDataTemplate DataType="{x:Type viewModels:MenuItemViewModel}"
ItemsSource="{Binding Path=MenuItems}">
<ContentControl cal:View.Model="{Binding}" />
</HierarchicalDataTemplate>
<Menu IsMainMenu="True"
ItemsSource="{Binding SharedViewModel.MenuItems}" />
SharedViewModel:
public class SharedViewModel : PropertyChangedBase
{
private List<MenuItemViewModel> _menuItems;
public List<MenuItemViewModel> MenuItems
{
get { return _menuItems; }
set
{
_menuItems = value;
NotifyOfPropertyChange(() => MenuItems);
}
}
}
Example of a TabViewModel updating the Menu:
protected override void OnActivate()
{
base.OnActivate();
SharedViewModel.MenuItems = new List<MenuItemViewModel>
{
new MenuItemViewModel
{
Header = "MainMenuItem1",
MenuItems =
new List<MenuItemViewModel>
{
new MenuItemViewModel {Header = "SubMenuItem1"},
new MenuItemViewModel {Header = "SubMenuItem2"},
}
},
new MenuItemViewModel
{
Header = "MainMenuItem2",
MenuItems =
new List<MenuItemViewModel>
{
new MenuItemViewModel {Header = "SubMenuItem1"},
new MenuItemViewModel {Header = "SubMenuItem2"},
}
}
};
}