i have a List with my services, and on item selected i'm passing service type to my activity ServiceDetail like this:
ServiceActivity
void item_selected(object sender, AdapterView.ItemClickEventArgs e) {
MenuContentItem selectedItem = (MenuContentItem)item[e.Position];
if(selectedItem.Title == "COLLO") {
var activity_go = new Intent(this, typeof(ServiceDetailActivity));
activity_go.PutExtra("service_type", "Collo");
StartActivity(activity_go);
}
if (selectedItem.Title == "SPALLA") {
var activity_go = new Intent(this, typeof(ServiceDetailActivity));
activity_go.PutExtra("service_type", "Spalla");
StartActivity(activity_go);
}
}
ServiceDetailActivity
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ServiceDetail);
//enable navigation mode to support tab layout
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
AddTab("Introduzione", Resource.Mipmap.Icon, new IntroduzioneFragment());
//intent data
string text = Intent.GetStringExtra("service_type") ?? "Data not available";
IntroduzioneFragment fragment = new IntroduzioneFragment();
// set data to pass to my fragment
Bundle bundle = new Bundle();
bundle.PutString("text", text);
fragment.Arguments = bundle;
}
// MY FRAGMENT - I would like "CUSTOM" my fragment "IntroduzioneFragment" like this:
class IntroduzioneFragment : Fragment {
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Menu.Tab, container, false);
var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView);
var imageView = view.FindViewById<ImageView>(Resource.Id.image_view);
imageView.SetImageResource(Resource.Mipmap.slide1);
// Get type of service
var test = Arguments.GetString("text");
if (test == "Collo") {
sampleTextView.Text = "is collooooo";
} else {
sampleTextView.Text = "is not collo";
}
return view;
}
}
I don't want create one activity for each service, i would like just have one "Service activity detail" and custom text and image by service type.
ERROR: when I select item service:
System.NullReferenceException - Object reference not set to an instance of an object. on:
var test = Arguments.GetString("text");
You have two ways of doing that.
If that is the activity that holds the fragment, you can call
this.Activity
inside fragment and just call any method of the activity after casting
AwesomceActivty castetActivity = (AwesomeActivity)this.Activity;
castetActivity.AwesomeMethod(12);
Or you can do that by using Delegates:
Define delegates in your Fragment class
namespace Awesome.Android {
public class AwesomeFragment : Fragment {
public delegate void OnAwesomePress (int number);
public event OnAwesomePress sendOnAwesomePressEvent;
}
}
You can assign it when you create a Framgent
AwesomeFragment fragment = new AwesomeFragment ();
fragment.OnAwesomePress += OnAwesomePress;
After that, you implement OnAwesomePress in your activity
private void OnAwesomePress (int number) {
}
Now, when you call sendOnAwesomePressEvent in your Fragment, that event will be passed to Activity.
sendOnAwesomePressEvent (10);
Related
I am completely new to Android and I have to submit this app to end my college degree, but I am having this problem where my fragment crashes my entire app.
To explain what I have done up until now is, I have a LoginActivity where I sent the userId through the Intent and have the id of the current user on my DashboardActivity (and I can display it), but in the DashboardActivity I have a bottom navigation bar that navigates to my FormFragment and my DataFragment.
Right now, want I would love to pass the userId value of the current user from the DashboardActivity to my DataFragment, so that I can display dynamically the user data according to the userId.
So with it, I found that the best option is to use bundle, but I don't now why (because I am completely new to this) my app crashes every time I switch from my FormFragment to my DataFragment.
Can you help me? I am desperate xD
This is my DashboardActivity code:
public class PainelActivity extends AppCompatActivity {
private Button buttonLogout;
private TextView textViewId;
private Object DashboardFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_painel);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
// gets the id from the Intent
Intent get = getIntent();
String userId = get.getStringExtra(LoginActivity.EXTRA_ID);
// Send the id to the Fragments
Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.nav_host_fragment, fragment).commit();
// see the id on the screen
textViewId = findViewById(R.id.textViewId);
textViewId.setText(userId);
// logout
buttonLogout = findViewById(R.id.logoutButton);
buttonLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
}
public void openMainActivity() {
Intent HomePage = new Intent(this, MainActivity.class);
startActivity(HomePage);
Toast.makeText(PainelActivity.this, "Terminou a sua sessão.",
Toast.LENGTH_LONG).show();
}
}
And this is my DataFragment code:
public class DashboardFragment extends Fragment {
INodeJS myAPI;
private TextView textViewResult;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
//Init API
Retrofit retrofit = RetrofitClient.getInstance();
myAPI = retrofit.create(INodeJS.class);
textViewResult = root.findViewById(R.id.text_view_result);
// gets the id from the activity
if (getArguments() != null) {
String userId = getArguments().getString("userId");
int uid = Integer.parseInt(userId);
Call<List<DataResult>> call = myAPI.executeGetData(uid);
call.enqueue(new Callback<List<DataResult>>() {
#Override
public void onResponse(Call<List<DataResult>> call, Response<List<DataResult>> response) {
if (response.code() == 200) {
List<DataResult> DATA = response.body();
for (DataResult data: DATA) {
String content = "";
content += "Data: " +data.getDta() + "\n";
content += "Hora: " +data.getHora() + "\n";
content += "Glicémia: " +data.getIndiceGlicemia() + "\n";
content += "Insulina: " +data.getInsulina() + "\n";
content += "Medicação: " +data.getMedicacao() + "\n\n";
textViewResult.append(content);
}
}
}
#Override
public void onFailure(Call<List<DataResult>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
return root;
}
}
Thank you and have a nice day!
(I just edited my code and updated my question, right now, my app doesn't crash but I don't see the data.)
While creating bundle:
Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_placeholder_id, dataFragment, "anyTagName").commit();
The To get the data in your fragment:
if (getArguments != null) {
String userId = getArguments().getString("userId");
}
When you create a new fragment you have an auto-generated function called newInstance.
So what you need to do is:
public static MyFragment newInstance(String param1, String param2) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putString("ARG_NAME", param1);
args.putString("OTHER_ARG_NAME", param2);
fragment.setArguments(args);
return fragment;
}
Then, in your activity:
String str1 = "foo";
String str2 = "bar";
MyFragment myFragment = MyFragment.newInstance(str1, str2);
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction()
.replace(R.id.nameOfActivityLayout, myFragment);
transaction.addToBackStack(null); // Add this line if you want to add the fragment to the back-stack
transaction.commit();
And back to your fragment, in your onCreate method :
String str1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
str1 = getArguments().getString("ARG_NAME");
}
}
The variable str1 will now have the value "foo" and you're free to use it in your fragment. Of course you can put other types in the bundle, like integers, booleans, arrays etc.
You are using the jetpack navigation component, so its easier than before.
You just have to pass the bundle to the navigation controller
Do:
navController.setGraph(R.navigation.graph, YOUR_BUNDLE);
then in your start fragment:
Bundle b = getArguement();
String id = b.Get string("I'd");
I am new to android and I am trying to call my MapFragment from adapter after on click using intent below is my code
Below is adapter code:
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final BusInfo info = getItem(position);
View view = LayoutInflater.from(context).inflate(R.layout.bus_only_list,null);
TextView busname;
busname = (TextView) view.findViewById(R.id.busname);
busname.setText(info.name);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pref = context.getSharedPreferences("busInfo",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("bus_name",info.name);
editor.commit();
Intent intent = new Intent(context, MapsFragment.class);
intent.putExtra("name",info.name);
context.startActivity(intent);>
}
});
return view;
}
I want to pass to mapfragment using intent but it redirect to MainActivity instead of MapFragment. How can I stop transferring to MainActivity?
Thank you.
A common pattern to passing a value to a Fragment is using newInstance method. In this method you can set Argument to fragment as a means to send the value.
First, create the newInstance method:
public class YourFragment extends Fragment {
...
// Creates a new fragment with bus_name
public static YourFragment newInstance(String busName) {
YourFragment yourFragment = new YourFragment();
Bundle args = new Bundle();
args.putString("bus_name", busName);
yourFragment.setArguments(args);
return yourFragment;
}
...
}
Then you can get the value in onCreate:
public class YourFragment extends Fragment {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the value from arguments
String busName = getArguments().getString("bus_name", "");
}
...
}
You can set the value to the Fragment from your activity with:
FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
YourFragment yourFragment = YourFragment.newInstance("bus_name_value");
fragTransaction.replace(R.id.fragment_place_holder, yourFragment);
fragTransaction.commit();
You can use the above codes to send the value in Fragment initialization.
If you want to set the value to the already instantiated fragment, you can create a method then invoke the method to set the value:
public class YourFragment extends Fragment {
...
public setBusName(String busName) {
// set the bus name to your fragment.
}
...
}
Now, In the activity, you can invoke it with:
// R.id.yourFragment is the id of fragment in xml
YourFragment yourFragment = (YourFragment) getSupportFragmentManager()
.findFragmentById(R.id.yourFragment);
yourFragment.setBusName("bus_name_value");
You cannot pass an intent to a Fragment. Try using a Bundle instead.
Bundle bundle = new Bundle();
bundle.putString("name", info.name);
mapFragment.setArguments(bundle)
In your Fragment (MapsFragment) get the Bundle like this:
Bundle bundle = this.getArguments();
if(bundle != null){
String infoName = bundle.getString("name");
}
As guys mentioned before:
1. Use callback or just casting on your context (your activity must handle changing fragments itself).
2. To change fragments use activity's FragmentManager - intent is used to start another activity.
Fragments Documentation
I want to call fragment method from another fragment, so I find relevant fragment in host activity and call method of this fragment. But some time I getting fragment null.
Suppose I want to save data of fragment A from all other fragment. There is 4 fragment like A, B, C and D. When I click save button from fragment A and B then it working fine but When I save data from fragment C and D then Fragment A getting null.
Here is my code :
HostActivity.cs
public class HostActivityView : MvxCachingFragmentCompatActivity<HostActivityViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
try
{
SetContentView(Resource.Layout.HostActivityView);
var toolbar = FindViewById<Toolbar>(Resource.Id.myToolbar);
if (toolbar != null)
{
// Toolbar will now take on default actionbar characteristics
SetSupportActionBar(toolbar);
}
// View Pager
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
{
// Add tabs in view pager
var fragments = new List<MvxFragmentStatePagerAdapter2.FragmentInfo>
{
new MvxFragmentStatePagerAdapter2.FragmentInfo("Ticket", typeof(Tab_Ticket), typeof(TicketEditViewModel)),
new MvxFragmentStatePagerAdapter2.FragmentInfo("Employee", typeof(Tab_Employee), typeof(EmployeeViewModel)),
new MvxFragmentStatePagerAdapter2.FragmentInfo("Response", typeof(Tab_Correspondence), typeof(ResponseViewModel)),
new MvxFragmentStatePagerAdapter2.FragmentInfo("Expense", typeof(Tab_Expenses), typeof(ExpenseViewModel)),
};
viewPager.Adapter = new MvxFragmentStatePagerAdapter2(this, SupportFragmentManager, fragments);
}
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
}
catch (Exception ex)
{
Mvx.Resolve<IUserInteraction>().Alert(ex.Message);
}
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.menu_accept:
Tab_Ticket tabTicket = (Tab_Ticket)SupportFragmentManager.FindFragmentByTag("Tab_Ticket");
if (tabTicket != null)
{
tabTicket.OnOptionsItemSelected(item);
handled = true;
}
break;
}
}
}
Here is my fragment Code :
Fragment.cs :
public class Tab_Ticket : MvxFragment<TicketEditViewModel>
{
public override View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.frg_tab_ticket, null);
return view;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.menu_accept:
this.ViewModel.CmdTicketSave.Execute(null);
return true;
break;
}
}
}
Please suggest me where I going wrong?
Thank you
Finally got the solution for this problem.
In host activity increase the view pager page limit. Because I have 5 fragments and default it display 3 fragments in support fragment manager. So sometime getting first fragment null. Added this line in my code and it working fine.
viewPager.OffscreenPageLimit = 6;
Here is more details about this issue.
I'm using Xamarin Studio and developing a small test project for Android.
I have an Activity with three Tabs on it, each Tab have a different Fragment. So far I got the hang of how to add Tabs and event handlers.
But when I rotate the screen, the default Tab I set is selected which causes the Fragment assigned to that Tab to be displayed.
Another problem I face is that when I change Tabs, I want to preserve the state of the previous Tab, so when I select it again it won't be rendered again. For example, one of my Tabs is a GridView which loads remote images in its cells. When I switch Tabs I don't want for the images to be loaded again.
My main Activity looks like this:
public class MainActivity : Activity
{
private ActionBar.Tab UploadImageTab;
private ActionBar.Tab ImgurTwitterTab;
private ActionBar.Tab RecentImagesTab;
private int selected_tab;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
if (bundle != null) {
selected_tab = bundle.GetInt ("selected_tab", 0);
Log.Debug (GetType ().FullName, "selected tab was " + selected_tab);
}
if (ActionBar != null) {
InitializeActionBar ();
}
SetContentView (Resource.Layout.Main);
}
protected override void OnSaveInstanceState (Bundle outState)
{
Log.Debug (GetType ().FullName, "Saving state tab selected " + selected_tab);
outState.PutInt ("selected_tab", selected_tab);
base.OnSaveInstanceState (outState);
}
protected void InitializeActionBar(){
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
AddTab (UploadImageTab, Resources.GetString (Resource.String.upload_image), Resource.Drawable.ic_upload, new UploadImageFragment(), 1);
AddTab (ImgurTwitterTab, Resources.GetString (Resource.String.imgur_twitter), Resource.Drawable.ic_com, new ImgurOnTwitterFragment(), 2);
AddTab (RecentImagesTab, Resources.GetString (Resource.String.recent_images), Resource.Drawable.ic_gallery, new RecentImagesFragment(), 3);
if (selected_tab == 0) {
Log.Debug (GetType ().FullName, "No value found");
ActionBar.SelectTab (UploadImageTab);
} else {
if (selected_tab == 1) {
Log.Debug (GetType ().FullName, "Selecting tab 1");
ActionBar.SelectTab (UploadImageTab);
} else if (selected_tab == 2) {
Log.Debug (GetType ().FullName, "Selecting tab 2");
ActionBar.SelectTab (ImgurTwitterTab);
}else if(selected_tab == 3){
Log.Debug (GetType ().FullName, "Selecting tab 3");
ActionBar.SelectTab (RecentImagesTab);
}
}
}
protected void AddTab(ActionBar.Tab tab, string tabText, int iconResourceId, Fragment fragment, int index){
tab = ActionBar.NewTab ();
tab.SetText (tabText);
tab.SetIcon (iconResourceId);
tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e) {
e.FragmentTransaction.Replace(Resource.Id.fragmentContainer, fragment);
if(ActionBar.SelectedTab.Position == 0){
selected_tab = 1;
}else if(ActionBar.SelectedTab.Position == 1){
selected_tab = 2;
}else if(ActionBar.SelectedTab.Position == 2){
selected_tab = 3;
}
Log.Debug(GetType().FullName, "selection is " + selected_tab);
};
ActionBar.AddTab (tab);
}
}
For starters I tried to save the selected Tab. But when I rotate the device, for some reason the TabSelected event on the first Tab (UploadImageTab in this case) is fired, causing the saved value I had to be overwritten.
On the example for my Fragment with a GridView, my code is like this:
public class RecentImagesFragment : Fragment
{
private GridView collectionView;
public List<Photo> photos;
public static float DENSITY;
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Console.WriteLine ("is this called every time I switch tabs");
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.RecentImagesTab, container, false);
DENSITY = Activity.Resources.DisplayMetrics.Density;
collectionView = view.FindViewById<GridView> (Resource.Id.collectionView);
collectionView.ItemClick += ItemClick;
photos = new List<Photo> ();
MakeRequest ();
return view;
}
public void ItemClick(object sender, AdapterView.ItemClickEventArgs args){
Console.WriteLine ("photo selected " + photos [args.Position].OriginalUrl);
Intent intent = new Intent (this.Activity, typeof(PhotoDetail));
intent.PutExtra ("url", photos [args.Position].OriginalUrl);
StartActivity (intent);
}
public void MakeRequest(){
var request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/gallery/hot/viral/0.json");
request.Headers.Add ("Authorization", "Client-ID " + "XXXXXXXXXXX");
request.Method = "GET";
Task<WebResponse> task = Task.Factory.FromAsync (
request.BeginGetResponse,
asyncResult => request.EndGetResponse (asyncResult),
(object)null);
task.ContinueWith (t => ReadStreamFromResponse (t.Result));
}
private void ReadStreamFromResponse(WebResponse response){
using (Stream responseStream = response.GetResponseStream ()) {
using (StreamReader sr = new StreamReader (responseStream)) {
string content = sr.ReadToEnd ();
Console.WriteLine (content);
try{
var json = JsonObject.Parse (content);
var array = json ["data"];
foreach (JsonObject o in array) {
string url = o ["link"];
bool isAlbum = o ["is_album"];
if (!isAlbum) {
var short_url = url.Insert (url.Length - 4, "s");
photos.Add (new Photo{ OriginalUrl = url, SmallThumbUrl = short_url });
}
}
} catch(Exception ex){
Console.WriteLine ("Error: " + ex.Message);
}
if (photos.Count > 0) {
Activity.RunOnUiThread (() => {
collectionView.Adapter = new ImageAdapter (this.Activity, photos);
});
}
}
}
}
}
When the view is created I make a HTTP request to Imgur for the latest images url, then I assign the List of Photo objects I create to my ImageAdapter that will download/render them. But these objects are lost when I switch Tabs.
How can I make sure I save the state of my Fragments? And how do I save the state of my Fragment's GridView adapter?
I was able to find an basic example here which helped me dealing with the situation I'm facing. I made the following changes to my code (comments will explain the functionality):
MainActivity.cs
public class MainActivity : Activity
{
private ActionBar.Tab UploadImageTab;
private ActionBar.Tab ImgurTwitterTab;
private ActionBar.Tab RecentImagesTab;
private int selected_tab;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Initialize Action Bar
InitializeActionBar ();
// Check if bundle is different from null, then load saved state and set selected tab
if (bundle != null) {
selected_tab = bundle.GetInt ("selected_tab", 0);
ActionBar.SetSelectedNavigationItem (selected_tab);
Log.Debug (GetType ().FullName, "selected tab was " + selected_tab);
}
}
// Save the selected tab
protected override void OnSaveInstanceState (Bundle outState)
{
Log.Debug (GetType ().FullName, "Saving state tab selected " + this.ActionBar.SelectedNavigationIndex);
outState.PutInt ("selected_tab", this.ActionBar.SelectedNavigationIndex);
base.OnSaveInstanceState (outState);
}
// Initialize Action Bar
protected void InitializeActionBar(){
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
// First big change
// Pass to AddTab method a tab instace, tab text, icon and a tag
AddTab<UploadImageFragment> (UploadImageTab, Resources.GetString (Resource.String.upload_image), Resource.Drawable.ic_upload, "upload");
AddTab<ImgurOnTwitterFragment> (ImgurTwitterTab, Resources.GetString (Resource.String.imgur_twitter), Resource.Drawable.ic_com, "tweets");
AddTab<RecentImagesFragment> (RecentImagesTab, Resources.GetString (Resource.String.recent_images), Resource.Drawable.ic_gallery, "recent");
}
// AddTab now handles generic types that inherit from Fragment
protected void AddTab<T> (ActionBar.Tab tab, string tabText, int iconResourceId, string tag) where T : Fragment{
tab = ActionBar.NewTab ();
tab.SetText (tabText);
tab.SetIcon (iconResourceId);
// tag will help us id this tab
tab.SetTag (tag);
// Get instance of Fragment if it exists
T existing = (T)FragmentManager.FindFragmentByTag (tag);
// Set listener for tab
tab.SetTabListener(new ActivityTabListener<T>(this, tag, existing));
ActionBar.AddTab (tab);
}
}
ActivityTabListener.cs
// Tab listener for generic type that inherits from Fragment
public class ActivityTabListener<T> : Java.Lang.Object, ActionBar.ITabListener where T : Fragment{
// Instance of current context
private Activity context;
// Reference to fragment to be displayed
private Fragment fragment;
// Name of Fragment class
private string fragmentName;
// Tag for tab
private string tag;
// Base constructor requires an Activity instance
public ActivityTabListener(Activity context){
this.context = context;
this.fragmentName = typeof(T).Namespace.ToLower() + "." + typeof(T).Name;
}
// Second constructor receives context, tag and existing fragment instance if available
public ActivityTabListener(Activity context, string tag, T existingFragment = null) : this(context){
this.fragment = existingFragment;
this.tag = tag;
}
// if fragment instance is null then create instance from generic type
// else just attach the fragment
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft){
if (fragment == null) {
fragment = (T)global::Android.App.Fragment.Instantiate (this.context, this.fragmentName);
// if there's a tag then add the fragment to its container and tag it
// else just fragment
if (this.tag != null) {
ft.Add (Resource.Id.fragmentContainer, fragment, tag);
} else {
ft.Add (Resource.Id.fragmentContainer, fragment);
}
} else {
ft.Attach (fragment);
}
}
// if fragment is not null then detach it
public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft){
if (fragment != null) {
ft.Detach (fragment);
}
}
public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft){
}
// if disposing the dispose of fragment
protected override void Dispose (bool disposing)
{
if (disposing)
this.fragment.Dispose ();
base.Dispose (disposing);
}
}
These are the important parts for making sure the state of each Fragment on the Activity is persistent when making a configuration change (changing tab, changing orientation, etc).
Now you only need for each Fragment subclass you create to retain their instance and whatever parameters you were using (a list filled by a HTTP request, an adapter, etc) to be reassigned to where they belong (DON'T REINITIALIZE YOUR VARIABLES OR YOU WON'T RETAIN THE SAME VALUES).
Each Fragment subclass must have the following on its OnCreate method:
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Don't call this method again
RetainInstance = true;
// whatever code you need on its first creation
}
Then you need to make sure your OnCreateView handles the logic to display the view with the data you want, for example if you have a fragment with a List View then you'd be wanting to have a reference to its adapter and its content, then when the view its being created check if any of those is null, if it is then you need to follow your logic to initialize them, else reassign them to the view that will be displayed:
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.some_layout, container, false);
some_list_view = view.FindViewById<ListView> (Resource.Id.some_list_view);
// since the state of this object is retained then check if the list that holds the objects for the list view is not null
// else then just reassing the adapter to the list view
if (some_list == null) {
some_list = new List<SomeObject> ();
// make a HTTP request, load images, create adapter, etc
} else {
some_list_view.Adapter = someAdapter;
}
return view;
}
With this you can avoid your fragments from losing their state when you change tabs or change orientation.
In Fragment Tab:
`#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt('tabSelected', viewPager.getCurrentItem());
}`
Inside onCreateView insert:
tabSelected=savedInstanceState.getInt("tabSelected", 0);
` #Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_config_assoc_tab, container, false);
viewPager = rootView.findViewById(R.id.viewpager_config);
viewPager.setPagingEnabled(false);
viewPager.setOffscreenPageLimit(0);
**if (savedInstanceState != null) {
tabSelected=savedInstanceState.getInt("tabSelected", 0);
}**
....
....
`
Now after rotating you will be tabSelected to the tab position. tabSelected is a global var.
I have my main activity actionbaractivity One where you can screenslide through some fragmets, on each fragment you have an imageView and a ListView where you can click any item and the image will change. Also in the menu options you have a button where you change to an almost exact activity: actiobbaractivity Two which also have this button to change to activity One
What I'm able to do is to keep the image when sliding the fragments, but unable to keep the fragments state's through the change of activities.
For example
I'm in activity One on fragment 3 with the image: "something". I click on the button to change to activity Two, I do things here and then, I click on the button to change to activity One and I want to see my fragment 3 with the image: "something" and not the default fragment 1 and default image
Im using ActionBarActivity, FragmentStatePagerAdapter and Fragment for each activity
Thanks for the help
According to the Activity and Fragment lifecycles (http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle and http://developer.android.com/guide/components/fragments.html#Lifecycle), the most reliable way of persisting states between activity/fragment changes is to use the default API for saving and restoring states:
When the activity/fragment is being dismissed (either because of a configuration change such as screen rotation or because the user requested to go to another activity/fragment), you can save its state in a Bundle object. When it is being created, you can restore its saved state, thus recreating a new instance exactly like the one the user left - so the user feels nothing has changed. This does not depend on the specific subclass of activity/fragment you are using.
I have implemented something like what you want: in my case, a fragment containing a menu with buttons that would each lead the user to another fragment containing a submenu with a "back" button. So if the user went from menu to submenu 1, then back to menu, then to submenu 2, then back to menu and finally again to submenu 1, I wanted that submenu 1 to appear just like the user has left it in the first time.
For that I have created:
1) an interface defining my submenu types, implemented by my activities so they could change between my submenus
2) a master generic class, which all my submenus would extend, that had a Bundle object to store their state
3) in my activities, I had an array of Bundle capable of storing one instance of each of my submenus (because I am only interested in restoring the last state, so I don't need more than one)
The interface (item 1):
public interface SubmenusManager {
public static enum Submenus {
ROOTMENU,
SUBMENU1,
SUBMENU2;
private static final int size = Submenus.values().length;
public static int size() {
return size;
}
public static int getId(Submenus test) {
switch(test) {
case SUBMENU1:
return 1;
case SUBMENU2:
return 2;
case ROOTMENU:
default:
return 0;
}
}
}
public void cloneCurrentSubmenuState(Parcelable toOverwrite);
public Bundle getLastStoredSubmenuState(Submenus submenu);
public void setCurrentSubmenuTo(Submenus submenu);
}
The generic class (item 2):
public class MenuFragment extends Fragment {
private Bundle menuData = new Bundle();
public static String RESTORE_MAIN_OBJECT = "restore_main";
public Bundle getMenuData() {
return menuData;
}
public Bundle cloneMenuData() {
return new Bundle(menuData);
}
public void setMenuData(Bundle menuData) {
this.menuData = menuData;
}
}
One of the activities (item 3):
public class ExampleAct extends FragmentActivity implements SubmenusManager {
/**
* instance variables
*/
private MenuFragment mMenu;
private Bundle [] menuData; // the Array of Bundles!
private static final String CONTAINER = "parcelable_container";
private static final String SUBMENU = "saved_submenu";
private Submenus curSubmenu = Submenus.ROOTMENU; // the default state is the ROOTMENU
private boolean restoreLastSavedState = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) { // first time creating this activity
menuData = new Bundle[Submenus.size()];
} else { // this activity has a saved state from before
// restore all the data from all the submenus
menuData = (Bundle[]) savedInstanceState.getParcelableArray(CONTAINER);
// restore the info about which is the current active submenu
curSubmenu = (Submenus) savedInstanceState.getSerializable(SUBMENU);
}
buildMenuFragment(true);
//(...) stuff
}
private void buildMenuFragment(boolean restoreState) {
// (re)builds fragment inside menu.
// restoreState flags whether activity should look for
// saved state data and restore it
restoreLastSavedState = restoreState;
switch(curSubmenu) {
// Eclipse warns you about which are the constants in your enum
case ROOTMENU:
mMenu = new FragmentRootMenu();
break;
case SUBMENU1:
mMenu = new FragmentSubmenu1();
break;
case SUBMENU2:
mMenu = new FragmentSubmenu2();
break;
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, mMenu)
.commit();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(SUBMENU, curSubmenu);
cloneCurrentSubmenuState(mMenu.getMenuData().
getParcelable(MenuFragment.RESTORE_MAIN_OBJECT));
outState.putParcelableArray(CONTAINER, menuData);
// (...) stuff
}
#Override
public void cloneCurrentSubmenuState(Parcelable toOverwrite) {
if (menuData == null) menuData = new Bundle[Submenus.size()];
if (toOverwrite != null)
mMenu.getMenuData().putParcelable(MenuFragment.RESTORE_MAIN_OBJECT, toOverwrite);
menuData[Submenus.getId(curSubmenu)] = mMenu.cloneMenuData();
}
#Override
public Bundle getLastStoredSubmenuState(Submenus forThisSubmenu) {
return
(menuData == null || !restoreLastSavedState) ? new Bundle() : menuData[Submenus.getId(forThisSubmenu)];
}
#Override
public void setCurrentSubmenuTo(Submenus toThisSubmenu) {
if (mMenu != null) {
cloneCurrentSubmenuState(mMenu.getMenuData().
getParcelable(MenuFragment.RESTORE_MAIN_OBJECT));
}
curSubmenu = toThisSubmenu;
buildMenuFragment(true);
}
One of the submenus (extension of item 2):
public class FragmentSubmenu1 extends MenuFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_submenu1, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init();
}
public void init() {
// (...) stuff
MyParcelableObject tmp = null; // MyParcelableObject is a class
// that implements Parcelable and stores
// relevant info to rebuild this menu
// from a saved state
SubmenusManager m = (SubmenusManager) getActivity(); // remember activity implements SubmenusManager
Bundle bnd = m.getLastStoredSubmenuState(SubmenusManager.Submenus.SUBMENU1);
if (bnd != null) tmp = bnd.getParcelable(MenuFragment.RESTORE_MAIN_OBJECT);
if (tmp == null) {
tmp = new MyParcelableObject();
tmp.buildFromScratch(); // initializes with default data
}
// back button
Button backToMainMenu = (Button) getView().findViewById(R.id.submenu1_back);
backToMainMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((SubmenusManager) getActivity()).
setCurrentSubmenuTo(SubmenusManager.Submenus.ROOTMENU);
}
});
// (...) stuff
}
}
The Root menu (extension of item 2):
public class FragmentRootMenu extends MenuFragment {
View myView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fragment_rootmenu, null);
return myView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init();
}
public void init() {
Button btnSubmenu1 = (Button) myView.findViewById(R.id.btn_call_submenu1);
btnSubmenu1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((SubmenusManager) getActivity()).
setCurrentSubmenuTo(SubmenusManager.Submenus.SUBMENU1);
}
});
Button btnSubmenu2 = (Button) myView.findViewById(R.id.btn_call_submenu2);
btnSubmenu2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((SubmenusManager) getActivity()).
setCurrentSubmenuTo(SubmenusManager.Submenus.SUBMENU2);
}
});
}
}
For that to work between activities, all you need to do is pass that object that stores the last state of all fragments (in my case, that would be Bundle [] menuData) to the activity that is being called through its Intent; you would recover it the same way as my ExampleAct did in its onCreate(). You could also wrap that Bundle [] inside a custom Parcelable object (very similar to my example MyParcelableObject; inside that one I had stuff like HashMap) if using an array is a problem.
Here how to pass a Parcelable between activities:
How to send an object from one Android Activity to another using Intents?