I want to make a startActivity to MapaQRF.java from HomeF.java that is inside navigationdrawer.
I mean, I want the onclick (button) do the same as the onNavigationDrawerItemSelected (in MapaQRF pocision)
fragment_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="cl.bikepurranque.www.bikeqrf.HomeF">
<Button
android:layout_width="190dp"
android:layout_height="wrap_content"
android:text="Mapa Paraderos QR"
android:id="#+id/btnMap"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:onClick="onClickMap"/>
</FrameLayout>
HomeF.java (Here's the problem)
public void onClickMap(View view){
Intent intent = new Intent(this.getActivity(), MapaQRF.class);
startActivity(intent);
}
NavDrawer.java
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = null;
switch (position){
case 0:
fragment = new MapaQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
break;
case 1:
fragment = new DetectarQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
break;
case 2:
Intent intent = new Intent(this, Login.class);
startActivity(intent);
break;
}
}
Fixed
The problem was that was calling an activity when I needed to call a fragment.
removed: "android:onClick="onClickMap"" on Button (fragment_home.xml)
added in HomeF.java
(onCreateView) Method
btnMap = (Button) rootView.findViewById(R.id.btnMap);
btnMap.setOnClickListener(this);
#Override
public void onClick(View v) {
if (v == btnMap){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.Fragment fragment = null;
fragment = new MapaQRF();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
}
Try to use getActivity()
public void onClickMap(View view){
Intent intent = new Intent(getActivity(), MapaQRF.class);
getActivity().startActivity(intent);
}
Related
I have a Navigation drawer activity and it contains couple of Fragments. My problem is I want to call the Navigation drawer activity class in button click which should have fragment B, by default navigation drawer has fragment A.
I am posting my code here:
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(),Navigation.class));
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, new FragmentB());
tx.commit();
finish();
}
});
Where content_frame is the area were I want to replace fragment B view..
try this approach
in your button click send extra data to check what fragment you need
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), Navigation.class);
intent.putExtra("SELECTEDVALUE", 2);//1 for fragament A use 2 for fragment B
startActivity(intent);
}
});
Now, in your oncreate() method of Navigation Activity
Bundle extras = savedInstanceState != null ? savedInstanceState : getIntent().getExtras();
int selectedValue = extras.getInt("SELECTEDVALUE");
switch (selectedValue) {
case 1:
goToFragment(new A(), false);
break;
case 2:
goToFragment(new B(), false);
break;
finally gotoFragment is...
private void goToFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container, fragment).commit();
}
Try below code;
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fragment = new FragmentB();
FragmentManager fm =getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
});
Just make an instance of FragmentB like this
FragmentB fragment - new FragmentB();
and then pass it like this .
tx.replace(R.id.content_frame, fragment);
tx.commit();
now you can access all the variables and methods of fragment from your activity using instance fragment. like this fragment.methodName();
Try this code:
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new FragmentB(), "fragmentB").addToBackStack(null).commit();
}
});
hope it'll solve your problem.
I went through some questions and made the changes as I thought were necessary, but the application still crashes every time I press the button to replace fragments. The fragments have the usual code and are just for simple layouts. On the launch, the activity will display the fragment I first add.
Here's my MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentLogin fl = new FragmentLogin();
final FragmentRegistration fr = new FragmentRegistration();
final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragcon, fr);
fragmentTransaction.commit();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(fl.isVisible())
{
fl.onActivityCreated(savedInstanceState);
fragmentTransaction.replace(R.id.fragcon, fr);
fragmentTransaction.commit();
}
else {
fl.onActivityCreated(savedInstanceState);
fragmentTransaction.replace(R.id.fragcon, fl);
fragmentTransaction.commit();
}
}
});
}
}
And here's my activitymain.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.aditya.registrationnlogin.MainActivity">
<RelativeLayout
android:id="#+id/fragcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.8"
android:layout_gravity="center_horizontal">
</RelativeLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="SWITCH" />
</LinearLayout>
You have to call fragmentManager.beginTransaction(); for every new transaction otherwise it will throw an exception
if(fl.isVisible())
{
//fl.onActivityCreated(savedInstanceState);
fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
fragmentTransaction.commit();
}else{
//fl.onActivityCreated(savedInstanceState);
fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
fragmentTransaction.commit();
}
and use setArgument to pass bundle without interrupting the fragment life cycle
// use this it will work sure dude !!
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentLogin fl = new FragmentLogin();
final FragmentRegistration fr = new FragmentRegistration();
final android.support.v4.app.FragmentManager fragmentManager =
getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragcon, fr);
fragmentTransaction.commit();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = null;
Class fragmentClass = null;
if(fl.isVisible())
{
fragmentClass = FragmentRegistration.class;
}
else {
fragmentClass = FragmentLogin.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
String backStateName = fragment.getClass().getName();
boolean fragmentPopped = fragmentManager.popBackStackImmediate
(backStateName, 0);
if (!fragmentPopped && fragmentManager.findFragmentByTag(backStateName) ==
null){ //fragment not in back stack, create it.
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment, backStateName);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
}
});
}
}
if(fl.isVisible())
{
//put your argument like this
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fr.setArguments(args)
fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
fragmentTransaction.commit();
}else{
//put your argument like this
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fr1.setArguments(args)
fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
fragmentTransaction.commit();
}
I am using the navigation drawer in Android Studio. When I select an item in navigation drawer I'm using the following code:
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
When I call my activities from the navigation drawer item selected the action bar disappears and activities open on full screen. How can I manage that the navigation drawer doesn't disappear?
If you want to persist navigation drawer you should change content fragment, instead of showing Activity.
In your case it would be, change
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent1);
with:
FragmentManager fragmentManager = ...
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.your_fragment_container_id, new HomeFragment())
transaction.commit();
dont use DayActivity_1.class activity use fragment instead and when you click on navigation item jst create fragment and replace it to the drawerlayout
Extend FragmentActivity in you main navigation activity class like below.
public class NavigationdrawerActivity extends FragmentActivity
Now use the below code to solve your problem
public void setContent(Fragment fragment) {
// Fragment fragment = new content_home();
FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
fragmentManager.setCustomAnimations(R.animator.enter_from_left, R.animator.exit_to_left);
// fragmentManager.beginTransaction()
fragmentManager.replace(R.id.mainContent, fragment).commit();
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Fragment homeActivityFragment = new HomeActivityFragment();
//if you want to pass data to fragment
//Bundle bundle = new Bundle();
//bundle.putString("id", "" + item.get("id"));
//homeActivityFragment.setArguments(bundle);
setContent(homeActivityFragment)
break;
case 1:
Fragment dayActivity_1Fragment = new DayActivity_1Fragment();
setContent(dayActivity_1Fragment)
break;
}
}
There is problem with my fragment, they are showed together, one on the second fragment. How to disapear, and only show one of them?
Definiton:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fr = new avc);
FragmentTransaction ft = ((TestingActivity)context).getFragmentManager().beginTransaction();
ft.replace(R.id.test, fr);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
And definiton of container below:
<FrameLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
You can use some thing like this. Make a function for showing a fragment and call each time this function with different parameter.
eg. If You want to show "HomeFragment" then call displayView(0) and if you want to show "FindPeopleFragment" then call displayView(1)
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I Recently used this to start A Fragment From Activity in Navigation Bar Activity (In The OnListItemClick() ) :
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
getActionBar().hide();
break;
case 5:
fragment = new WhatsHotFragment();
break;
case 6:
fragment = new MyFragment();
break;
case 7:
fragment = new Views();
break;
case 8:
fragment = new editText();
break;
The problem is that The fragment is not opening on top of Main Activity on Click of Button after
Instantiating the fragment with its default constructor .
But Now I'm trying to the same but its not working :
MainActivity.java
XML of MainActivity has A button As :
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/buttonflat"
android:onClick="startFrag"
android:layout_width="230dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:text="Button" />
and in Java Of MainActivity:(On Click Of Button )
public void startFrag(View v)
{
fragment = new Frag_FAB();
}
java of fragment:
public class Frag_FAB extends Fragment {
public Frag_FAB() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find_people,container,false);
return view;
}
}
Xml oF Fragment :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#010008"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:text="#string/stuff"
android:textColor="#color/highlighted_text_material_dark"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp" />
</RelativeLayout>
add the fragment transaction after the switch statement:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
your code must be something like:
switch(position){
...
...
...
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
getActionBar().hide();
break;
case 5:
fragment = new WhatsHotFragment();
break;
case 6:
fragment = new MyFragment();
break;
case 7:
fragment = new Views();
break;
case 8:
fragment = new editText();
break;
...
}
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment); //* Here you add the fragment! :)
You need to add it to the layout via a FragmentTransaction:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
You have to add your fragment to the FragmentManager. Here is an example :
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();