How can I have an item ("R.id.nav_code" for example) automatically selected when I open a page ?
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_code) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new code())
.commit();
} else if (id == R.id.nav_documentation) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new documentation())
.commit();
} else if (id == R.id.nav_number) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new number())
.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
If you want to start default fragment everytime your activity start then add this code into your activity onCreate Method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
code_Fragment code=new code_Fragment(); //Create Object Of your Fragment
transaction.replace(R.id.FragmentLoginRegistration,code);
transaction.commit();
}
Related
SO, I'm creating this basic app that contains different fragments and I want that when the user presses the back button on his phone, The app returns to the home fragment.
Main Activity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public void book(View view)
{
Toast.makeText(this, "You tried to book", Toast.LENGTH_SHORT).show();
}
public void submit(View view)
{
EditText editText = (EditText) findViewById(R.id.feedbackText);
if (editText.getText().toString().isEmpty())
{
Toast.makeText(this, "Please type something first", Toast.LENGTH_SHORT).show();
}
else
{
editText.setText("");
Toast.makeText(this, "Thank You for your feedback", Toast.LENGTH_SHORT).show();
closeKeyboard();
}
}
private void closeKeyboard()
{
View view = this.getCurrentFocus();
if (view != null)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
}
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar, searchToolbar;
NavigationView navigationView;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Menu search_menu;
MenuItem item_search;
Button submitBtn;
MaterialSearchView searchView;
EditText email;
EditText password;
TextView titleText;
int counter = 0;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);
return true;
}
public void guest(View view)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
submitBtn = findViewById(R.id.submitBtn);
titleText = findViewById(R.id.toolbarTitle);
searchToolbar = (Toolbar)findViewById(R.id.search_toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
searchView=(MaterialSearchView)findViewById(R.id.search_view);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
titleText.setText(R.string.home);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
drawerLayout.closeDrawer(GravityCompat.START);
if (item.getItemId() == R.id.home)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
titleText.setText(R.string.home);
}
if (item.getItemId() == R.id.myOrders)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new MyOrders());
fragmentTransaction.commit();
titleText.setText(R.string.myOrders);
}
if (item.getItemId() == R.id.refer)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Refer());
fragmentTransaction.commit();
titleText.setText(R.string.refer);
}
if (item.getItemId() == R.id.contact)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Contact());
fragmentTransaction.commit();
titleText.setText(R.string.contact);
}
if (item.getItemId() == R.id.feedback)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Feedback());
fragmentTransaction.commit();
titleText.setText(R.string.feedback);
}
if (item.getItemId() == R.id.settings)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Settings());
fragmentTransaction.commit();
titleText.setText(R.string.setting);
}
if (item.getItemId() == R.id.terms)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Terms());
fragmentTransaction.commit();
titleText.setText(R.string.terms);
}
return true;
}
}
Please help me to figure this out.
using onBackPress inside your Activity try this
#Override
public void onBackPressed() {
if (!(getSupportFragmentManager().findFragmentById(R.id.container) instanceof Home)) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
titleText.setText(R.string.home);
} else {
super.onBackPressed();
}
}
I created a Drawer activity, right now i have MainActivity and RegisterFragment. XML and other things are done right now i can open RegisterFragment with Navigation Drawer but how can i show that RegisterFragment when application starts?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
//unnecessary code block removed
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.nav_home) {
} else if (id == R.id.nav_gallery) {
fragment = new RegisterFragment();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_tools) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
As i said it working well basically i want to open RegisterFragment when application starts.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
// change fragment at onCreate method
changeFragment(new RegisterFragment());
}
private void changeFragement(Fragment fragment){
if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
}
}
//unnecessary code block removed
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.nav_home) {
} else if (id == R.id.nav_gallery) {
fragment = new RegisterFragment();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_tools) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
changeFragment(fragment);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
put same code
fragment = new RegisterFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
in onCreate of your activity
I know that this subject has been already discussed, but I have difficulties with implementing navigation between fragments. I want to return to a previous fragment or main activity using the back button. Now, the back button only closes the navigation drawer or exits the app. I know I should use addToBackStack but I don't know exactly how.
My MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("infoGmina");
ImageView gminaImageView = (ImageView) findViewById(R.id.gminaImageView);
ImageView weatherImageView = (ImageView) findViewById(R.id.weatherImageView);
ImageView partlyCloudImageView = (ImageView) findViewById(R.id.partlyCloudImageView);
partlyCloudImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PogodaFragment fragment = new PogodaFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.replacableConstraintLayout, fragment);
transaction.commit();
}
});
weatherImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WeatherFragment fragment = new WeatherFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.replacableConstraintLayout, fragment);
transaction.commit();
}
});
gminaImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AnnouncementListFragment fragment = new AnnouncementListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.replacableConstraintLayout, fragment);
transaction.commit();
}
});
ParseAnalytics.trackAppOpenedInBackground(getIntent());
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_gmina) {
AnnouncementListFragment listFragment = new AnnouncementListFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.replacableConstraintLayout, listFragment).commit();
setTitle("Gmina");
} else if (id == R.id.nav_restauracja) {
} else if (id == R.id.nav_sklep) {
} else if (id == R.id.nav_weather) {
WeatherFragment weatherFragment = new WeatherFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.replacableConstraintLayout, weatherFragment).commit();
setTitle("Jakość powietrza");
} else if (id == R.id.nav_pogoda) {
PogodaFragment pogodaFragment = new PogodaFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.replacableConstraintLayout, pogodaFragment).commit();
setTitle("Pogoda");
} else if (id == R.id.nav_onas) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
When we want to change Activity we can use:
Intent i = new Intent(this، ActivityTwo.class);
startActivity(i);
When i use it in some Activity it work correctly
But it dose not work when I use it in navigation drawer project. I think it dose not work beacuse Test class extends Fragment. in AppCompatActivity it work.
public class Test extends Fragment {
public Test() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_test , container, false);
button= (Button) v.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent((getActivity(), ActivityTwo.class);
startActivity(i);
}
});
return v;
}
}
In my MainActivity I change code:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment=null;
if (id == R.id.nav_camera) {
Test contactUsFragment=new Test();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,contactUsFragment).commit();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
ActivityTwoss=new ActivityTwo();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,ss).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
You create a xml container? If so, use a Fragment, not an Activity, for the FragmentManager
In my MainActivity I change code:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment=null;
if (id == R.id.nav_camera) {
Test contactUsFragment=new Test();
FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,contactUsFragment).commit();
} else if (id == R.id.nav_gallery) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.fragment_conteiner, new Test(), "Test");
transaction.commit();
} else if (id == R.id.nav_slideshow) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
MainActivity xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="br.com.onuse.finds.Activity.MainActivity"
tools:showIn="#layout/fragment_conteiner"
android:id="#+id/fragment_conteiner">
</FrameLayout>
I think you want to start a new Activity from your Fragment using the following sentence:
Intent i = new Intent((getActivity(), ActivityTwo.class);
startActivity(i);
Well, that won't work. You should start your Activity from your current activity (not from your Fragment). You may use something like this:
Intent i = new Intent((getActivity(), ActivityTwo.class);
getActivity().startActivity(i);
Or you can do that in one line from your Fragment:
getActivity().startActivity(new Intent(getActivity(), ActivityTwo.class));
Let me know if that works for you!
I am having a problem. I have an activity that implements several fragments. Now I need to update a textview of one of the fragments, I have the string in the activity, so I need to pass this string to the fragment and update that textview.
NavigationDrawer.java --> this is the activity
String user = "";
InicialPage inicialpage = new InicialPage();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naviagation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
user = extras.getString("KEY"); // TODO this is the value that i need to pass to the fragment InicialPage
}
Bundle bundle = new Bundle();
bundle.putString("USER", user);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
android.support.v4.app.FragmentTransaction fragmenttransaction =
getSupportFragmentManager().beginTransaction();
fragmenttransaction.replace(R.id.container, inicialpage);
fragmenttransaction.commit();
//inicialpage.setUsernameTextView(user);
DrawerLayout drawer1 = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer1.closeDrawer(GravityCompat.START);
TextView userNameTextView = (TextView)findViewById(R.id.usernametext);
//userNameTextView.setText(user);
android.app.Fragment currentFragment = getFragmentManager().findFragmentById(R.id.InicialPage);
}
#Override
public void onBackPressed () {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.naviagation_drawer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected (MenuItem item){
// Handle navigation view item clicks here.
int id = item.getItemId();
android.support.v4.app.FragmentTransaction fragmenttransaction =
getSupportFragmentManager().beginTransaction();
if (id == R.id.InicialPage) {
fragmenttransaction.replace(R.id.container, inicialpage);
fragmenttransaction.commit();
} else if (id == R.id.HistoryItem) {
fragmenttransaction.replace(R.id.container, new Historic());
fragmenttransaction.commit();
} else if (id == R.id.FriendsItem) {
fragmenttransaction.replace(R.id.container, new Friends());
fragmenttransaction.commit();
} else if (id == R.id.PointsItem) {
fragmenttransaction.replace(R.id.container, new Points());
fragmenttransaction.commit();
} else if (id == R.id.BookBikeItem) {
fragmenttransaction.replace(R.id.container, new BookBike());
fragmenttransaction.commit();
} else if (id == R.id.MessagesItem) {
fragmenttransaction.replace(R.id.container, new Messages());
fragmenttransaction.commit();
}else if(id == R.id.Mapdebug)
{
Intent maps = new Intent(this,MapsActivity.class);
startActivity(maps);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void FrameClicked(View view) {
//send user name to chat
Intent i = new Intent(this, Chat.class);
i.putExtra("USER", user);
startActivity(i);
}
public void addFriend(View view) {
LinearLayout principalLayout= (LinearLayout) findViewById(R.id.idFriendsVertical);
LinearLayout secondaryLauout= new LinearLayout(this);
TextView tx= new TextView(this);
EditText et= (EditText) findViewById(R.id.ADD);
String edittx= String.valueOf(et.getText().toString());
if (edittx.equals("")) {
}else
{
tx.setText(edittx);
tx.setTextSize(22);
tx.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.setMargins(0, 20, 0, 10);
secondaryLauout.addView(tx, params);
principalLayout.addView(secondaryLauout);
}
et.setText("");
}
}
InicialPage.java --> this is the fragment
public class InicialPage extends Fragment {
SQLiteDatabase db;
View view;
TextView usernameTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_inicial_page, container, false);
usernameTextView = (TextView) view.findViewById(R.id.usernametext);
return view;
}
If I understand your question correctly, you need to add inicialpage.setArguments(bundle); before your replace and commit in your fragment transaction.
Then, inside your Fragment you call
Bundle bundle = getArguments();
String bundleText = bundle.getString("USER");
to retrieve it.
You were already creating a bundle so I'm assuming that's the data you want to send. You have to attach that bundle to the Fragment before your Fragment Transaction though.
Inside your Activity:
Bundle bundle = new Bundle(); //create the bundle
bundle.putString("USER", user); //attach data to the bundle
inicialpage.setArguments(bundle); //set the bundle on the fragment
//perform fragment transaction
fragmenttransaction.replace(R.id.container, inicialpage);
fragmenttransaction.commit();
Inside your Fragment:
//retrieve the bundle
Bundle bundle = getArguments(); //retrieve the bundle
String bundleText = bundle.getString("USER"); //get the data on the bundle
//find view & set the text
usernameTextView = (TextView) view.findViewById(R.id.usernametext);
usernameTextView.setText(bundleText);
This type of problem can be solved using Localbroadcast Receiver .Write the following code in your fragment. Something like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("custom-event-name"));
return view;
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
usernameTextView.setText(message);
Log.d("receiver", "Got message: " + message);
}
};
then pass the user value by adding this following code in your activity when you want to pass
Intent intent = new Intent("custom-event-name");
intent.putExtra("message", user);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);