Any similar function for fragment like onActivityResult() - android-fragments

In my application I have scenario like,
When user click a button in FragmentA, it will show FragmentB. And when a button clicked on FragmentB it will go to FragmentC.
While navigating from A->B->C, I will set my activity title respectively as "A", "B" & "C".
This flow I did using,
public class FragmentC
onCreateView(){
mTxtHeaderTitle.setText(getString(R.string.fragC));
}
}
Now the question is, how I can set the title bar to "C" while user presses backkey and navigate back to B, ( C -> B )?
Is there any method callback available to do this?
onFragmentResult() as like onActivityResult() in Activity?
Please help.

You can use the onResume() callback method of Fragment to set the title.
onResume() is called by the system whenever the Fragment is in focus and interacting with the user.

Related

Why isn't MessagingCenter.Subscribe working?

In Xamarin forms, I Subscribe in OnAppearing and Unsubscribe in OnDisappearing. But it still calling the callback for every instance.
protected override void OnAppearing(){
if (isGoingBack)
MessagingCenter.Subscribe<PhoneNumberVerificationPajModal, string>(this, "Phone.Verify", codeSendRequest);
}
protected override void OnDisappearing(){
if (isGoingBack)
MessagingCenter.Unsubscribe<PhoneNumberVerificationPajModal>(this, "Phone.Verify");
}
Truth is calling Unsubscribe works. But, in my code I have a condition when the click on a button I open a Page. As that page is the one sending the message, when the user click the button I set isGoingBack = false. When I Press the back button and coming back to the page, I can Unsubscribe and Subscribe get called. But going to the next the callback event get call the same number of time I created a new page. But the truth is I Unsubscribe whenever I leave the page.
in your sample you are not passing the arg type to the unsubscribe method
try unsubscribing with the same type parameter you are subscribing with.
so instead of
MessagingCenter.Unsubscribe<PhoneNumberVerificationPajModal>(this, "Phone.Verify");
try
MessagingCenter.Unsubscribe<PhoneNumberVerificationPajModal, string>(this, "Phone.Verify");

How to write a code to get data from one form to another form by using button?

I have created a command button in the CustGroup form action pane.
I have added a new base enum edt field to both the CustGroup and CustTable tables and forms.
When you click on the button the data that was previously changed in the CustGroup table must be reflected in the cust table form.
I have written code in button on click event handler but it's not updating.
What to do, any suggestions?
If I understand your question correctly, you want to transfer a change of a new field in a customer group to all customers that share this customer group.
This kind of mass data update is usually not done by code in a form, because that code is executed on the client tier, which results in a bad performance. Instead, you should create a class that is set to execute on the server tier. If you create a main method for this class, you can easily create an action menu item for it, which let's you easily integrate the call to this class as a button in the CustGroup form.
In the main method you can access the CustGroup record for which the button was clicked via the Args object. This gives you the value of your new field that was changed. With this value, you can then use code similar to the following to update your customers:
public void updateCustomersWithNewCustGroupFieldValue(CustGroup _custGroup)
{
CustTable custTable;
ttsBegin;
while select forUpdate custTable
where custTable.CustGroup == _custGroup.CustGroup
{
custTable.MyNewEnumField = _custGroup.MyNewEnumField;
if (custTable.validateWrite())
{
custTable.update();
}
else
{
error('Please implement some error handling');
}
}
ttsCommit;
}

In Kotlin ..How I can return from fragment to MainActivity

in my program I have MainActivity and many fragments..
I try the following code to return from fragment to MainActivity by
onBackpressed() method
override fun onBackPressed() {
if(drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
}
else if (fragment != null) {
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
}
else {
super.onBackPressed()
}
}
My first problem is:
it working good with Drawer and also open MainActivity
but program not closed ..these main that
super.onBackPressed()
not working ..why
My second problem is:
after else If I need to use
getActivity().onBackpressed()
instead of the old one..
Thanks All
Activities navigate to Activities via onBackPressed(). Fragments have to reside in an Activity (They are basically sub-Activities), so it doesn't make sense to navigate from a Fragment to an Activity via super.onBackPressed(). You should be navigating from Fragment to Fragment, or if you forgo Fragments then Activity to Activity.
To navigate back to the previous Fragment:
activity?.fragmentManager?.popBackStack()
To navigate to the previous activity:
activity?.finish()
or
onBackPressed()
or, from the activity if you have overriden the onBackPressed() method:
super.onBackPressed()
Without more context to your code I can't say either why it seems your final else statement is never called. It seems like you've got a bug with your if else statement as super.onBackPressed() would provide the desired result of closing whatever activity you were in (MainActivity?).
else if (fragment != null) {
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
}
My guess is it has something to do with you creating another instance of MainActivity. On first back pressed you close the drawer. On the second you create another instance of MainActivity and navigate to it. On the third, even if super.onBackPressed() gets called it will navigate back to the first instance of MainActivity where fragment will probably never be null unless you're specifically assigning such, so on the fourth you create another instance of MainActivity and navigate to it. This is a loop that will never navigate back from the first MainActivity.
Suggestions: However you're displaying MainActivity, convert it to a Fragment and handle it accordingly. Another approach is instead of creating another instance of MainActivity hide the fragmentView and show the MainActivity view. I don't suggest setting your fragment to null as the fragmentManager may throw and error, so you should also change your if else logic to check for something else. Say maybe fragment.view.visibility == View.Visible if you go the route described.
For your first question as far as I can understand you should use
val activity = activity as MainActivity
activity.onBackPressed()
because super for your fragment is not MainActivity.
The easiest and most concise way (Kotlin):
requireActivity().addOnBackPressedCallback(viewLifecycleOwner,
OnbackPressedCallback{
startMainActivity() // Your action here...
true
}
)

How can I create a button on a form in a J2ME application?

I attempt to make a simple "hello world" application, where on clicking the button , it prints a string "hello world". How can I add a button on a form?
I need to create a button, on which when I click it can produce a string. How can I add a button without using canvas in j2me?
There is an API for this, but you better think twice whether you really need it.
API is desctibed in the Appearance modes section for lcdui Item objects
The StringItem and ImageItem classes have an appearance mode attribute that can be set in their constructors. This attribute can have one of the values PLAIN, HYPERLINK, or BUTTON. An appearance mode of PLAIN is typically used for non-interactive display of textual or graphical material. The appearance mode values do not have any side effects on the interactivity of the item. In order to be interactive, the item must have one or more Commands (preferably with a default command assigned), and it must have a CommandListener that receives notification of Command invocations...
A StringItem or ImageItem in BUTTON mode can be used to create a button-based user interface...
Note that this section also explains cases when using button appearance might be problematic:
...This can easily lead to applications that are inconvenient to use. For example, in a traversal-based system, users must navigate to a button before they can invoke any commands on it. If buttons are spread across a long Form, users may be required to perform a considerable amount of navigation in order to discover all the available commands. Furthermore, invoking a command from a button at the other end of the Form can be quite cumbersome. Traversal-based systems often provide a means of invoking commands from anywhere (such as from a menu), without the need to traverse to a particular item. Instead of adding a command to a button and placing that button into a Form, it would often be more appropriate and convenient for users if that command were added directly to the Form. Buttons should be used only in cases where direct user interaction with the item's string or image contents is essential to the user's understanding of the commands that can be invoked from that item.
From the class diagram I found in an old J2ME book, and which is online at http://www.stardeveloper.com/articles/display.html?article=2002121101&page=2 it seems that J2ME don't do buttons. Well no need for them on an old mobile phone.
Just create a "hello" command and add it to a menu or form. The system will then put it on whatever button is available on your device. For touch screen devices that probably turns it into something clickable.
Here's the code
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloWorld extends MIDlet implements CommandListener {
private static final String HELLO_WORLD = "Hello, World!!";
private Form form= new Form ("");
private Command exit= new Command("Exit", Command.EXIT, 0x01);
private Command ok= new Command("OK", Command.OK, 0x01);
private Command hello= new Command("HELLO", Command.SCREEN, 0x01);
private TextBox textBox= new TextBox("Hello World", HELLO_WORLD, HELLO_WORLD.length(), TextField.UNEDITABLE);
public HelloWorld() {
this.form.addCommand(exit);
this.form.addCommand(hello);
this.form.setCommandListener(this);
this.textBox.addCommand(ok);
this.textBox.addCommand(exit);
this.textBox.setCommandListener(this);
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException { }
protected void pauseApp() { }
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(this.form);
}
public void commandAction(Command c, Displayable d) {
if (c == this.exit) {
this.notifyDestroyed();
}
if(c == this.ok) {
Display.getDisplay(this).setCurrent(this.form);
}
if(c == this.hello) {
Display.getDisplay(this).setCurrent(this.textBox);
}
}
}

How to trigger an action from a NSTableCellView in view based NSTableView when using bindings

I'm facing a problem with a view-based NSTableView running on 10.8 (target is 10.7, but I think this is not relevant).
I'm using an NSTableView, and I get content values for my custom NSTableCellView through bindings. I use the obejctValue of the NSTableCellView to get my data.
I added a button to my cell, and I'd like it to trigger some action when clicked. So far I have only been able to trigger an action within the custom NSTableCellView's subclass.
I can get the row that was clicked like this, using the chain:
NSButton *myButton = (NSButton*)sender;
NSTableView *myView = (NSTableView*)myButton.superview.superview.superview;
NSInteger rowClicked = [myView rowForView:myButton.superview];
From there I don't know how to reach my App Delegate or controller where the action is defined.
As I am using cocoa bindings, I do not have a delegate on the NSTableView that I could use to trigger my action.
Do you have any idea how I could talked back to controller ?
Many thanks in advance!
Although you are using bindings you can still set your controller as the delegate for your tableview in the interface builder.
I see that you already are able to access the table view from inside your cell. The next task must be simple, just set the table view delegate as the target for your button's action.
Thanks for your question, I also will be triggering an action from a button on a NSTableView. Your question helped to put me on the correct path.
First to address the your solution to finding which row number my NSTableView is on. I was able to find it without knowing the button, in my custom NSTableView I installed the following as a first attempt:
- (NSInteger)myRowNumber
{
return [(NSTableView*)self.superview.superview rowForView:self];
}
this works fine, however it is less than robust. It only works if you already know specifically how deep you are in the view hierarchy. A more robust and universal solution is:
- (NSInteger)myRowNumber
{
NSTableView* tableView = nil;
NSView* mySuperview = self;
do
{
NSView* nextSuper = mySuperview.superview;
if (nextSuper == nil)
{
NSException *exception =
[NSException exceptionWithName:#"NSTableView not found."
reason:[NSString stringWithFormat:#"%# search went too deep.",
NSStringFromSelector(_cmd)] userInfo:nil];
#throw exception;
}
if ([nextSuper isKindOfClass:[NSTableView class]])
tableView = (NSTableView*)nextSuper;
else
mySuperview = mySuperview.superview;
} while (tableView == nil);
return [tableView rowForView:self];
}
this not only works at the NSTableView level, but works with anything installed at any level above it, no matter how complex the view hierarchy.
As to the unanswered part of your question, I established an IBOutlet in my class and using interface builder tied if to my files owner (in my case my document class). Once I had a reference to the class I was sending my message to, and the row number, I call the function. In my case the call required that I pass the row number it originates from.
[self.myDoc doSomethingToRow:self.myRowNumber];
I tested this and it works at various levels of the view hierarchy above NSTableView. And it functions without having to have the row selected first (which appears to be assumed in Apples documentation).
Regards, George Lawrence Storm, Maltby, Washington, USA
Use rowForView: and the responder chain
To respond to a control's action embedded within an NSTableCellView, the control should issue the action to the First Responder. Alternatively, File Owner is possible but this is more tightly coupled.
Use rowForView: within the action method to determine which row's control issued the action:
- (IBAction)revealInFinder:(id)sender {
NSInteger row = [self.tableView rowForView:sender];
...
}
The action is implemented within any of the responder chain classes. Most likely, this will be your subclassed NSWindowController instance. The responder could also be the application delegate; assuming the delegate has a means to talk to the NSTableView.
See Apple's example TableViewPlayground: Using View-Based NSTableView and NSOutlineView to see this in action.
Suhas answer helped me.
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "EDIT_CELL_VIEW"), owner: self) as? SymbolManagerCell {
if let editButton = cell.subviews[0] as? NSButton {
editButton.target = cell // this is required to trigger action
}
return cell
}
return nil
}

Resources