java


How can I show this Toast message only if I click the second Tab?


I have a navigation bar with three tabs each tab is a Fragment.
I wrote in the onCreateView method of my second fragment this toast message:
public class SecondFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.second_layout,container,false);
Toast.makeText(getContext(), "Test", Toast.LENGTH_LONG).show();
return rootView;
}
What I want is that only if I click this tab the message displays.
But the problem is that if I start the application the message displays directly, but why?
Here is my MainActivity.java:
public class MainActivity extends AppCompatActivity{
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the DrawerLayout and NavigationView
*/
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
}
}
And (I think this is Important for this question) my TabFragment:
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
if(position == 0){
return new FirstFragment();
}
if(position == 1){
return new SecondFragment();
}
if(position == 2){
return new ThirdFragment();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "First";
case 1 :
return "Second";
case 2 :
return "Third";
}
return null;
}
}
}
So where do I have to say Toast.makeText(getContext(), "Test", Toast.LENGTH_LONG).show(); to display it only by pressing on the second tab?
You can use the code for terminating the toast once it is shown so that it will not be visible when you click on another fragment.
try this one
toast = new Toast(getContext());
toast.cancel(); //this will prevent toast from displaying multiple times
Hope it will work
Wait! if you have multiple tabs then each tabs must be containing individual fragment and for each fragment there is java class and a layout file for the same then the coding part will be inside onCreate method of java file and xml/layout part will be in .xml file thats it.

Related Links

I ran out of ideas for this nested for loop
JDBC physical vs logical
Storm - Class Cast Exception while using the parameter in next bolt
How to get list of file names inside a specific folder in stash repository using stash rest API in Java
Spring boot - Externalize config properties
Access Parent Field with Child Object Java
overwriting objects in arraylist when adding temp object
Java Spring MVC 404 not found
Two tables in one ( java )
Pom.xml that run only in Windows
Connect Java to Access DB on network drive
What is the best way to navigate a complex tree of dissimilar objects?
Input to assign value to double variable
Eclipse doesn't generate new class files when running
Is this proper encapsulation for java? I'm confused
Java: How would I declare an undefined number of object with user inputted data?

Categories

HOME
ionic-framework
pdf
osgi
cloud
atom-editor
coq
path-finding
view
adb
yarn
umd
ojdbc
bs4
amazon-cloudformation
medical
ssl-client-authentication
offline
collectd
vaadin7
django-simple-history
worldwind
libtiff
contextmenu
google-cloud-speech
textfield
abi
zurb-foundation-6
underflow
spark-jobserver
phpfox
facebook-apps
unboundid
http-get
code-contracts
calibre
avcapturesession
overriding
bower-install
stacked
jquery-bootgrid
mixture-model
twitch
hilbert-curve
ansible-playbook
hp-ux
jvm-languages
password-encryption
turbogears
.net-4.6.2
sencha-touch-2.3
flickr-api
revapi
datastax-startup
carrot
google-perftools
android-cursor
memory-alignment
flutterwave
jspdf-autotable
abcpdf9
infix-notation
ado.net-entity-data-model
plottable.js
testng-dataprovider
qgraphicsview
whois
pervasive-sql
vhd
reactive-banana
execute
endeca-workbench
collapse
simple-framework
deis
operation
createprocessasuser
composite
kineticjs
rake-task
gadt
tablelayout
relocation
responsive-slides
doskey
marmalade
fmod
pyhdf
apc
gwt-rpc
seed
specification-pattern
tridion-worldserver
transactionscope
gwt-ext
psi
django-tagging
xtype
krl
xsdobjectgen
firefox-5
meego
sef
adrotator
mtj
virtual-functions
genealogy
grid-system
windows-live-messenger
ajax-forms

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