android


Bundle in a fragment created


Is there anyway to assign a Bundle to a fragment already created?. I tried something like this:
Bundle mArgumentos = new Bundle();
mArgumentos.putString("id", id);
this.setArguments(mArgumentos);
But I get the error: Fragment already active.
I'm just wondering if I can achieve this without overriding the onBackPressed() method.
My purpose with this is because I have a form for registry a new item. From here I can go to another fragment (lets call it fragment_B) with the new id of the item (I call fragment_B with a bundle assigning the new id). When the user presses back button, he gets to the previous form but the id is missing (this is because the form goes to the previous state, like empty), so if he tries to go to fragment_B again, he cant because the id.
Obviously the user cant go to fragment_B if the form has not been save. So the user saves the form and then press the button to go to fragment_B with its new id:
FragmentB fragment = new FragmentB();
Bundle mArgumentos = new Bundle();
mArgumentos.putString("id",id);
fragment.setArguments(mArgumentos);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment, tag);
ft.addToBackStack(tag);
ft.commit();
Any suggestions?.
Edit:
The fragments are not in the same activity. When I create a new item and press button save I should be in a "edit mode". This means that after I create the item I can come back and edit it. I'm just using the same fragment to edit and create. So, when I create the item, press the button to go to fragment_B and then press back button, I should be like "edit mode" of this item. This means that from fragment_B to the previous I should pass the id of the item created. That's why I wanna know if I can do this without overriding the onBackpress method.
Edit2:
If there is no other way to achieve this, I was thinking in override the onBackpress method like this:
//get the fragment where I'm staying on.
Fragment fragmentActual = this.getSupportFragmentManager().findFragmentById(android.R.id.tabcontent);
//get the tag of the fragment
String fragmentTag = fragmentActual.getTag().toString();
if (fragmentTag.equals("tagFragmentB")){
Fragment fragmentA = new FragmentA();
Bundle mArguments = fragmentActual.getArguments();
String id = mArguments.getString("id");
if(id != null){
fragmentA .setArguments(mArguments);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment, "fragmentA");
ft.addToBackStack("fragmentA");
ft.commit();
}
}else{
super.onBackPressed();
}
This is working but as you have notice, this change the order of the fragments called by the user and this should be invisible for the user. So, this forces me to put more code in order to get the calls that the user did.
I'm kind of new with this, so maybe I have not the "Android thinking". If is there a better way to do what I want, I'm open to everything. I just want to do the thinks right and dont have problems in the future just because a user pressed a back button.
Well in my particular situation a made it work doing the following: I noticed that when I fill the form, saved it, then go to fragmentB and finally press back button, the information of my form was retained. So I put a hidden etidText in my form:
<EditText
android:id="#+id/textEditHiddenId"
android:visibility="gone"
android:inputType="number" />
When the button save is pressed, I set the content of this editText:
etIdHidden.setText(id);
When the activity is created, according to http://developer.android.com/guide/components/fragments.html, the method onResume is called after the onCreateView. So, I get the value of my textEditHiddenId in this method:
#Override
public void onResume() {
super.onResume();
String id= etIdHidden.getText().toString();
if(id.equals("")==false){
mArgumentos=new Bundle();
mArgumentos.putString("id", ""+id);
}
};
The first time, the content of textEditHiddenId is empty, but when backbutton is pressed and the onResume method is called, I get the content initialized and I can override my Bundle.
Simplest solution ever..

Related Links

Recyclerview not clickable
Ionic - Obfuscated app not running on the phone, why?
Android MediaCodec decoder ,carry custom info with input video frames
How to Insert value into Android SQLite DataBase without OnClick?
Stream real time videos in 3D, using stereoscopy on android
Converting Gif to an Image ANDROID
Communicating with Android Wear within iOS
React-Native Android: Pass Object as prop for Native Android UI Component
Container for Floating Action Button + Snackbar
How to create a PreferenceScreen in a PreferenceActivity?
Programmatic Theme Change Not Working
Get JSONObject from retrofit2 response
Looking for some way to monitor the error and crash on Unity [duplicate]
How can i dynamically size buttons according to the screen size?
Issue with collapsing toolbar in marshmallow and above
Cannot Fix a header above RecyclerView in Android

Categories

HOME
gdb
polymer
github-for-windows
alexa-skills-kit
pda
3d
fftw
seo
limit
jetty
code-coverage
nfs
character
plist
openbugs
frequency-distribution
internationalization
searchview
powershell-v5.0
react-toolbox
aggregation
zxing
jquery-callback
draft-js-plugins
interbase
autodesk-designautomation
eviews
jmockit
glib
catch-unit-test
baidu-map
sim-card
password-protection
opensmpp
numberpicker
xv6
odoo-website
branching-and-merging
plr
vaadin-charts
x++
serde
vugen
geotiff
x-ray
pair-programming
azure-cli
context-sensitive-grammar
ngmaterial
monkey
flyout
mathcad
renaming
jtwig
kango-framework
spreadjs
sklearn-pandas
np-spring
toran-proxy
qt5.2
pypiserver
wiredep
dock
ember-cli-rails
user-permissions
fits
grouping-sets
infosphere-spl
ghcjs
administration
vbe
code-duplication
hidapi
cffile
swfobject
patricia-trie
nokia-s40
jsr286
jquery-address
entitydatasource
massive
tmx
collectionviewsource
undefined-index
opml
cuda.net
google-wave
application-planning

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App