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

Can't access localhost from Android Windows Emulator (Visual Studio 2015 / Xamarin)
Error:Execution failed for task ':app:packageDebug'. > !zip.isFile()
Android: Failure [INSTALL_FAILED_VERSION_DOWNGRADE], but version is already higher than published
Android Service cannot start because of a ClassNotFoundException from a class that doesn't even exist anymore
FingerPrint cannot work in Service
Android app to record from 3,5 mm jack [closed]
Download Google Drive Document When user select a file and click on open with my app
Google Advertising ID is always null after opting out and back in
Do i need an a recycleview or an adapter for card view? having issue with going from cardview to activity
java.util.zip.ZipException: duplicate entry
Android Firebase update only some fields
Circular Shape within Rectangle
How to stop action bar move up
Scrolling down leads to activate the pull down refresh option
Firebase notifications: activity doesn't launch when status bar notification is clicked
Remove item from SharedPreference when removed from Listview

Categories

HOME
spark-streaming
jmeter
angular2-material
youtube-data-api-v3
numbers
grafana
pdfbox
opendj
rds
parameters
interpolation
iis-6
crystal-reports-2013
lanczos
performancepoint
fhir
alamofire
mxgraph
endpoint
plink
package-managers
dimensional-modeling
easyphp
asp.net-mvc-viewmodel
xmonad
lilypond
microsoft-ocr
blade
visual-studio-extensions
easeljs
surveymonkey
pic32
amazon-quicksight
exchangewebservices
paperjs
eviews
scatter3d
dst
utorrent
weather
cart
spring-security4
login-script
installshield-le
activexobject
salesforce-communities
openalpr
sessionstorage
xdocreport
password-protection
bc
ogc
trigonometry
portforwarding
congestion-control
http-request
rfc3339
mesosphere
serde
spring-data-couchbase
excel-dna
filehandle
libjpeg
structuremap4
restore
donations
atlas
hadoop-2.7.2
envi
macaulay2
installshield-2010
observablecollection
android-wake-lock
kango-framework
sklearn-pandas
halcon
grass
false-sharing
concept-insights
pyobjc
xmltype
cross-join
apache-spark-1.3
structuremap3
webfinger
jama
unix-socket
grails-domain-class
xml-dsig
jquery-slider
authlogic
git-repo
android-audiorecord
multitasking
ie8-compatibility-mode
inputmismatchexception
tlf
windows-98
pdfviewer
pyunit
stress
cgimage
virtualpathprovider
box2d-iphone
cuteeditor
dataform
unattended-processing
cac
silent
globals
eclipse-marketplace
web-garden
isapi-extension
echo3
interop-domino
localizable.strings
cardspace

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