I'm making an android application with a navigation drawer.
I want this application to connect to the internet, and I need to put a label that contains informations about the connection into a fragment and some buttons to send information in another. I would like the label to update constantly, also if its fragment isn't shown.
My question is:
Is it possible to programmatically edit the elements of a fragment when it isn't shown?
as an answer to your comment, yes and this is how it works
Bundle bundle = new Bundle();
bundle.putString("key", "actualValue");
MyFragment fragment = new MyFragment();
fragment.setArguments(bundle);
// start your fragment
and then in your fragment onCreate or onCreateView
Bundle bundle = getArguments();
if(bundle !=null){
String actualData = bundle.getString("key");
TextView myTextView = (TextView)view.findViewById(R.id.myText);
myTextView.setText(actualData);
}
Related
i'm calling fragment via button within the main activity with this (i'm using bundle to send data here)
String ScoreText ;
Scooore myFragment = new Scooore();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Bundle data = new Bundle();
data.putString("score", scoreText);
myFragment.setArguments(data);
fragmentTransaction.replace(R.id.showScore, myFragment).commit();
then inside my score fragment i have button there , and i wanna know what code shall i put inside the onClicklistener method there so that button wpild take me back to the main activity
i can use new intent and go back there or even hide the frame layout using setVisibility on it
, but ig maybe there's better options, right ?
In your MainActivity, add transaction to back stack:
fragmentTransaction.beginTransaction().addToBackStack("scoore");
and then in your fragment pop it from the back stack:
getActivity().getSupportFragmentManager().popBackStack();
I'm trying to continue the flow of app within bottomsheet.
Case : When a user clicks any brand suppose "Levis" then it should display the brand products i.e. another fragment(containing Products data) within the same bottomsheet only.
Problem: the another fragment(containing Products data) is opening in a seperate frame not in the existing bottomsheet.
My code on adapter holder click event:
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String id = ld.getBrand_id();
Bundle bundle = new Bundle();
bundle.putString("somekeyvalue", String.valueOf(id));
Fragment fragment = new ProductFragment();
fragment.setArguments(bundle);
FragmentManager fragmentManager = ((FragmentActivity) ct).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
I'm using Google material bottomsheet.
code to start bottomsheet:
BrandFragment bottomSheet = new BrandFragment();
bottomSheet.show(getChildFragmentManager(),
"ModalBottomSheet");
you are loading new ProductFragment() into R.id.frameLayout, which probably belongs to Activity. if you want to open new Fragment inside another one then define container, which belongs to this first Fragment. also use then getChildFragmentManager, not getSupportFragmentManager
btw. ensure that R.id.frameLayout is unique and declared ONLY inside "parent" Fragment, not in Activity
This is now bugging me a lot.
I want to get to an activity from a Fragment without disturbing the reusability of the fragment. That mean I cant directly start an Intent from within the fragment to a the activity the fragment is currently attached to.
Here is the code under question
//This is the code inside the fragment
public void onStart() {
super.onStart();
View view = getView();
switch (position){
//case 1 is the case when the user clicks the ADD List Item from a ListFragment.
//Position is the position of ADD in the List Fragment.
case 1:{
Intent intent = new Intent(/*what exactly should I put here?*/ ,
/*this is where the reference to activity the fragment is attached to goes.
But we dont know what activty the fragment is attached to, as it is reusable
and may get attached to different activity at different times*/);
}
case 2:{
//this is the case when user decides to view the entered text in the array list.
TextView textView = (TextView)view.findViewById(R.id.display_name);
int size = workout.arrayList.size();
Object[] array = workout.arrayList.toArray();
for(int i=0;i<array.length;i++){
textView.setText((String)array[i] + "\n");
}
}
}
}
I feels like the data may be insufficient, although I am not sure what else to provide, sorry about that.
If more data is required, tell me.
Inside a Fragment we get the Context for an Intent using:
Intent intent = new Intent(getActivity(),AnyActivity.class);
OR
Intent intent = new Intent(getView.getContext(),AnyActivity.class);
The AnyActivity() can be any activity including the one currently having the Fragment.
You can open the same activity like this..
Intent intent = new Intent(getActivity(),SameActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
UPDATE To reuse the fragment for different values pass arguments to the fragment...during transaction..
Bundle bundle = new Bundle();
bundle.put("className", "SameActivity");
fragment.setArguments(bundle);
in fragment get the class name from bundle ... different class will pass different arguments as bundle..
In OtherActivity bundle would look like this..
Bundle bundle = new Bundle();
bundle.put("className", "OtherActivity");
fragment.setArguments(bundle);
I know that data can be transferred from an Activity to another Fragment using Bundle. I have done some research and I found out that Bundle works only when the Activity commits a Fragment transaction to the Fragment where the data has to be send.
My question is: Is there any way by which I can send data from the activity to another fragment without committing fragment transaction?
I have attached an image which explains the scenario:
Here is the Main Activity (User.java):
protected void onCreate(Bundle savedInstanceState)
{
ActionBar ab = getSupportActionBar();
ab.hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
TextView txt = (TextView) findViewById(R.id.textView5);
Intent intent = getIntent();
String Name = intent.getStringExtra("UserName");
txt.setText("Logged in as "+Name);
Bundle bundle = new Bundle();
bundle.putString("message", Name );
user_profile up = new user_profile(); //This is the Fragment where I want to send data(which is "Name")
up.setArguments(bundle);
user_home uh = new user_home(); //This is the Fragment that is added
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container,uh);
ft.commit();
}
Here is the Second Activity where I want to send data(Known as "user_profile.java")
Bundle bundle2 = this.getArguments();
String N = bundle2.getString("message");
Toast.makeText(getContext(), N , Toast.LENGTH_SHORT).show();
Is there any way by which I can send between activity and fragment other than using bundle?
I'm not sure what kind of data you want to pass exactly.. but this could help you https://github.com/greenrobot/EventBus
What I do in such situation is
Create a interface in Activity
Implement that interface in the Fragment
Pass that data, it will be received in the interface method inside fragment, and you'll also know when it is received.
Google has demonstrated fragment to activity communication, just check that code and follow my instructions here for activity to fragment communication.
https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
I am fairly new to android development, and I'm trying to make a small app that lists names with a few action buttons placed beside each name. My plan was to loop through the list of names, and add a fragment for each one, forming a list of them on the screen.
Right now I was trying to test out the following code. I want to add the fragment "fragTest", to "fragPlaceHolder" in the activity.
This is what my code looks like:
//Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//Replace the contents of the container with the new fragmentTest
for (int i = 0; i < listLength; i++){
ft.add(R.id.fragPlaceHolder, new fragmentTest());
}
//Complete the changes
ft.commit();
However when I do this, only one fragment ever seems to be added. And if it did work, how would I pass a name as an argument?
Use fragmentTransaction.AddToBackStack(null) in your code.
You can send data to fragment with intent:
Bundle bundle=new Bundle();
bundle.putString("message", "From Activity");
//set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);
Recieve data in fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext=getArguments().getString("message");
return inflater.inflate(R.layout.fragment, container, false);
}