Custom Intent-Filters in android - android

What i am currently try to achieve is:
Add a record in one activity and show it on the other activity.
And on the click of the edit button on the show activity it takes back user to the first page for editing the data.
The problem is that i cannot use Intent.putextra() for this because the add page is linked with other so it gives me error for this.
Thought of using shared preferences as well,but i don't think it would help me much, since if a value is set in shared preference it would be available on load of the activity so i wont be able to know from where i entered the activity. so thought of using intent-filters for it.
can anyone help me with this ?
And i dont want to create another class for edit purpose.

Why not check for...
<pre>
<code>
if (getIntent() != null && getIntent().getSerializableExtra("Id") != null)
{
//get the data here becuz it's not null here....
}
</code>
</pre>
also just note that getIntExtra() also exists...

Related

How to show "up" / "back" button in Xamarin.Forms?

I am trying to work out how to show the "up" arrow in Xamarin.Forms without a pushing a page onto the stack. I.E. I just want to perform an action when the back button is pressed. I am completely stuck on this so any help would be appreciated.
I have tried creating a custom renderer which handles a view property called DisplayHomeAsBack. Which in the renderer calls the following:
FormsAppCompatActivity context = ((FormsAppCompatActivity)Forms.Context);
Android.Support.V7.App.ActionBar actionBar = context.SupportActionBar;
if (actionBar != null)
{
actionBar.SetDisplayHomeAsUpEnabled(element.DisplayHomeAsBack);
}
Unfortunately it seems this does absolutely nothing, even though all online tutorials and stackoverflow question for android suggest this method.
The plan is that I can then use the "OnBackButtonPressed" override in MasterDetailPage, which should allow me to perform this action. Unfortunately displaying the back button has been the larger hurdle so far!
Any idea of a better way to do this or how I can get the current mechanism to work?
EDIT
I have created a project and uploaded it to this question on the Xamarin support forums, if it helps.
http://forums.xamarin.com/discussion/comment/186330#Comment_186330
Sorry to keep you waiting so long!
Warning that I did not actually run this code and changed it from my own so I would be surprised if it worked perfectly without some changes.
So below should add a back button where there was not one before (so like when there is not really a page to go back to) and then we will add a custom action to perform when it gets pressed.
I would suggest you push a new page onto the stack without using animation so it is transparent to the user and also makes all of this much simpler, but if you absolutely do not want to do that, the below method should work.
MainActivity:
//Use this to subscribe to the event which will create the back button
public override bool OnCreateOptionsMenu(IMenu menu) {
if(menu != null && App.AppMasterPage != null) { //You will need this to make sure you are on your MasterDetailPage, just store a global reference to it in the App class or where ever
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
Xamarin.Forms.MessagingCenter.Subscribe<string>(this, "CreateBackButton", stringWeWillNotUse => { //Use this to subscribe to the event that creates the back button, then when you want the back button to show you just run Xamarin.Forms.MessagingCenter.Send<string>(this, "CreateBackButton")
ActionBar.DisplayOptions = ActionBarDisplayOptions.ShowTitle | ActionBarDisplayOptions.ShowHome | ActionBarDisplayOptions.UseLogo | ActionBarDisplayOptions.HomeAsUp; //You may need to play with these options to get it working but the important one is 'HomeAsUp' which should add the back button
});
} else {
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
}
return base.OnCreateOptionsMenu(menu);
}
Now the next step is do do a custom action when it is pressed. I think you can either override OnBackPressed() or OnOptionsItemSelected() in MainActivity or maybe you can override the MasterDetailPage method. I am not sure.
Which ever one works for you, inside of that override, I would simply check to see if you are on your App.AppMasterPage like we did above, and if so, send a MessagingCenter message which your App.AppMasterPage has already subscribed to in order for it to handle the custom action.
If you get stuck let me know!
I know it sounds like a bit of a hack, but the best "solution" I have found so far is to add a page behind the current page (behind the root) so it is not visible. Then when the user presses the back button, handle it by removing that page.

How can I identify what Fragments are current visible on screen?

I have forked an Android application and wish to change the behavior of a specific fragment. How can I use Android Studio to identify what fragments are currently displayed on-screen so that I can navigate to the correct .java file in the source?
When you add the fragment to your transaction add a tag,
fragmentTransaction.replace(android.R.id.container, homeFragment, "HOME_FRAGMENT");
After that, to check if fragment is currently displayed is easy :
HomeFragment homeFragment = (HomeFragment)getFragmentManager().findFragmentByTag("HOME_FRAGMENT");
if (homeFragment != null && homeFragment.isVisible()) {
// do whatever you want :)
}
you can look through the Logcat with your app's package name then usually there are some related calling such as onActivityDestroyed, from which you can see which fragment that you need to work on

Android SavedPrefrerences loading?

I am trying to make an app(http://pastebin.com/uWkP6XNY) that when you press a button, creates a custom sms message. The user can go to a second activity (http://pastebin.com/MK2NPV5R) thats full of edit-texts that when saved, will bring back strings to be used to change the custom sms.
The issues I am facing is how I initalize my variables with whats in savedpreferences. I put this in my onCreate method.
smsintroduction = (sp.getString("intro", "")); //these are both strings initalized at the top
smsbody = (sp.getString("body", ""));
On start up, since it can't get "intro" from the dictionary, it goes to a null string. I want to be able to use my save() function in my second activity to save, which I Think I already do, but be able to change my two strings above.
I put the code above to set the strings in a method that completes the finalized textbody, but it keeps giving me emptystrings.
The only thing that gets created is "!", as shown in finishedtext().
It looks like in your MainActivity in onCreateOptionsMenu you are overriding the sp member previously set in onCreate with
sp = getSharedPreferences("prefs", 0);
Try removing that line or setting those shared prefs to a different instance member.

is it possible to Set selected value of second select based on specific values chosen from first select in android?

I have two Tabbar, First is for the ApplicationTAB View and second is for the SettingTAB.
Now i have to set the Sound On/Off from the SettingAtb. If i have selected the Sound On then on ApplicationTab there is sound to be Play. And if i have Selected Off then the Sound should not be play on the ApplicationTab. So what should i have to do to implement like this.
And if it is possible then let me know how i can able to do this settings.
Please Help me regarding this logic.
Thanks.
As Googling i got the proper word for it.
I want to implement to set the ToggleButton Value from one tab to another tab to be affected.
Means i want to set the Something like Sound On/off from one tab for the another tab.
If there is any Demo Project then let me know.
From what I undertood from your question you are trying to pass a value between 2 activities according to my understanding. And your sound on/off should be stored in shared preferences so that you can later refer them when the app starts the next time.
This is a clear explanation of Shared Preferences from google. This will allow you to store data as long as the app is not deleted.
So when you set the sound on/off flag you can save it in the sharedpreferences and when you go back to the first tab, you can fetch the flag from shared preferences in the onCreate and do what is necessary for the sound.
Here is a simple tutorial from a SaiGeetha's blog
you can try this
getSharedPreferences(Settings.SHARED_PREFS_NAME, MODE_PRIVATE)..getString(PREF_NAME, defaultValue);
or use other type of storage
documentation here
You can have interface defined in the tab host class. Implement the interface and pass it to the settings tab. Call its callback method from the settings tab whenever the setting changes.
In the callback implementation, stop/start the sound.
class tabhost
public interface soundSettingsCallback {
public void onSoundSettingsChanged(boolean bStop);
};
apptab.setSoundSettingCallback(new soundSettingsCallback {
public void onSoundSettingsChanged(boolean bStop) {
// stop/start sounds.
}
});

Switching Views/layouts

I have a problem that I can't seem to find the solution to.
I have an app that loads the main.xml file on startup, of course. In it are several buttons, and I want the buttons to take me to a different XML file. I just used setContentView(R.layout.newlayout.xml) method for that, and it works great.
The problem comes in after that. If I reference any of the buttons or other objects in the new layout, the app won't even finish loading before it errors out and closes on the emulator. However, if I take all references to objects out, the app runs fine.
I can navigate TO the new layouts, but their buttons can't do anything. Do I need to create a separate Java file for each layout? Or am I doing it all wrong? I'm trying to be as specific as I can. I suppose you could say I need to have different "pages" in my app as a website would.
I think what you are trying to do is best solved using multiple java files, each one defining it's own android Activity.
While it is possible to have multiple layouts/views in a single activity, this will generally make the code more complex and harder to read/debug in the future. By having each 'screen' in its own file, it will be a bit easier to manage all the different views you need to juggle.
The buttons and views only can refer to those mentioned in the current SetContentView() file..
u can test this by creating a button and initialising to an R.id... without setting the content view.. U will get a force close..
so if u change the XML file u shud initialise stuff again....
Ok, for anyone out there with the same problem and haven't figured out how to do it, as I said in my comment on ylebre, my Coworker and I have finally discovered how to do it. First off, we added
implements OnClickListener
to the class, after
extends Activity
then, we created a new java file, and at the beginning of the file it called
setContentView(R.layout.newlayout);
instead of main. Then, we made a button as follows:
Button button1 = (Button) findViewById(R.id.button01;
button1.setOnClickListener(this);
then later in the code:
public void onClick(View v) {
switch(v.getId()) {
case R.id.button01:
startActivity(new Intent(this, NEWJAVAFILE.class));
break;
}
}
And that's it! We just copied and pasted that code into NEWJAVAFILE, changed the names and such, and we were able to navigate freely back and forth. As ylebre said, all of the code for the new activity is in the NEWJAVAFILE.java. OH and don't forget to add the name of the java file to the manifest inside the tags:
<activity android:name=".NEWJAVAFILE">
</activity>
it all seems so simple now!

Categories

Resources