java


How to move one fragment to another fragment in android


I am developing an app contains fragments. I cannot move one fragment to another fragment.
When i use this code (reference from android value passing from fragment to fragment using recyclerview not working)
SpeedDialFragment fragobj = new SpeedDialFragment();
Bundle bundle=new Bundle();
bundle.putString("message", "From Activity");
fragobj.setArguments(bundle);
switchFragment(R.layout.fragment_page, fragobj);
It is not working.
My codes are shown below.
MainActivity java:
public class MainActivity extends AppCompatActivity implements PageFragment.OnFragmentInteractionListener {
ViewPager viewPage;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
startService(new Intent(this, LockScreenService.class));
}
private void initView() {
initViewPager();
}
private void initViewPager() {
viewPage = (ViewPager) this.findViewById(R.id.viewpage);
viewPage.setOffscreenPageLimit(3);
viewPage.setCurrentItem(1);
viewPage.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
#Override public Fragment getItem(int position) {
if (position < 2) {
return PageFragment.newInstance("", "");
} else {
return new AmapFragment();
}
}
#Override public int getCount() {
return 3;
}
});
}
#Override public void onFragmentInteraction(Uri uri) {
}
public void loadFragment(int id, Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(id, fragment, fragment.toString());
ft.addToBackStack(null);
ft.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.happiness.lockscreen.MainActivity"
>
<android.support.v4.view.ViewPager
android:id="#+id/viewpage"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v4.view.ViewPager>
</RelativeLayout>
fragment_page.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"
>
<Button
android:text="Speed Dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonSpeedDial" />
</FrameLayout>
PageFragment.java
public class PageFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
Button button;
Context context;
private OnFragmentInteractionListener mListener;
public PageFragment() {
}
public static PageFragment newInstance(String param1, String param2) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_page, container, false);
button = (Button) view.findViewById(R.id.buttonSpeedDial);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("SpeedDialFragment", "clicked : ");
/*SpeedDialFragment fragment = new SpeedDialFragment(); // replace your custom fragment class
Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
bundle.putString("key","value"); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(viewID,fragment);
fragmentTransaction.commit();*/
SpeedDialFragment fragobj = new SpeedDialFragment();
Bundle bundle=new Bundle();
bundle.putString("message", "From Activity");
fragobj.setArguments(bundle);
switchFragment(R.layout.fragment_page, fragobj);
}
});
return view;
}
public void switchFragment(int id, Fragment fragment) {
if (context == null)
return;
if (context instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) context;
mainActivity.loadFragment(id, fragment);
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(
context.toString() + " must implement OnFragmentInteractionListener");
}
}
#Override public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Please help me.
SpeedDialFragment.java
public class SpeedDialFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public SpeedDialFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment PageFragment.
*/
// TODO: Rename and change types and number of parameters
public static PageFragment newInstance(String param1, String param2) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext=getArguments().getString("message");
Log.d("SpeedDialFragment", "strtext : "+strtext);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_speeddial, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(
context.toString() + " must implement OnFragmentInteractionListener");
}
}
#Override public void onDetach() {
super.onDetach();
mListener = null;
}
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
if(context==null) return; You have not initialized your context object.
Try this
public void switchFragment(int id, Fragment fragment) {
if (getActivity() instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.loadFragment(id, fragment);
}
}

Related Links

How do I use a custom SSLContextFactory in a Restlet application running on Jetty?
why this code runs out of order
how do i use the information from a deep link url that opens an activity in android? [closed]
How to store a splitted string into an array?
JMeter: java.net.URISyntaxException: Malformed escape pair at index
Can an Actor handle click and key down events?
Why does Hibernate return random result?
Why isn't my GUI displaying?
Spring Security - Custom Pre Auth filter using java config
Java: retrieving and printing out items from an arraylist using getters
MigLayout - I have two scrollpanes, I want the edge between them to be draggable. How would I do this?
Apache Camel Rest high response time on first request
Java stream limit and skip behaviour when unordered and parallel
Displaying a JScrollPane with data from String
Post on Facebook pages via Graph API with Facebook4j
Exception in thread “main” java.lang.NoClassDefFoundError:

Categories

HOME
clips
gitlab
tizen
plone
view
include
cvs
leon
callback
django-imagekit
ios-charts
session-variables
gorm
spring-xd
quicklook
correlation
commonmark
static-libraries
flask-wtforms
carthage
seaborn
undefined
sms-gateway
jqwidget
nhibernate-envers
grails3
object-detection
orleans
web-sql
hammerspoon
dxf
crystal-reports-2010
selectedindexchanged
internet-explorer-9
windows-error-reporting
elasticsearch-ruby
windowbuilder
gsoap
sql-server-2012-express
google-rich-snippets
javascriptcore
key-value-observing
contact-form
noraui
textmate
language-concepts
normal-distribution
mapdb
bitbucket-pipelines
swisscomdev
multilingual
eclipse-scout
xml-documentation
appcompat
hibernate-tools
filepicker
termination
upstart
atomicity
rails-routing
dropbox-php
sqlclient
nodebb
r-forge
pdfclown
zendesk-app
webdriverjs
account-kit
chord-diagram
topbeat
visual-c++-2008
angular-cache
gridpane
radtreelist
sts-springsourcetoolsuite
mikroc
gridview-sorting
testng-dataprovider
ptrace
freedesktop.org
spim
jms2
teamcity-8.0
purge
browser-bugs
e10s
facebook-graph-api-v2.4
gui-test-framework
typo3-neos
floating-point-precision
geos
quantlib-swig
coverflow
sqlperformance
html-editor
word-processor
getmessage
pendrive
work-stealing
eclipse-templates
doh
spyware
heartbeat
tomcat-valve
yetanotherforum
disclosure
radcombobox
dmx512
wise
getresponsestream
routedevent
ti-dsp
boost-smart-ptr

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