Reputation: 1
The MainActivity has 2 Fragments(FragmentA and FragmentB). FragmentA has a TextView and FragmentB has another Activity(GymActivity). The GymActivity has a Button which should hide the TextView in FragmentA when Clicked!
I tried doing this by this below method But it didn't work! It only works if the TextView is in FragmentB.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static MainActivity instance;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity.main);
instance = this;
}
public static MainActivity getInstance(){
return instance;
}
public void myMethod(){
// my method...
}
}
AnotherClass.java
public Class AnotherClass() {
MainActivity.getInstance().myMethod();
}
This is my code!
Fragment A
public class AFragment extends Fragment {
TextView txtview;
private static AFragment instance;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_A, container, false);
txtview = view.findViewById(R.id.A_txt);
instance = this;
return view;
}
public static AFragment getInstance() {
return instance;
}
public void myMethod() {
txtview.setVisibility(View.GONE);
}
}
Fragment B
public class BFragment extends Fragment {
public CardView CvGym;
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_B, container, false);
CvGym = (CardView) view.findViewById(R.id.gym);
CvGym.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), GymActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});
return view;
}
}
Now in GymActivity
public class Gym extends AppCompatActivity {
Button buttonX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Gym);
buttonX = (Button)findViewById(R.id.btn_X);
getbtnX();
}
public Button getbtnX() {
buttonX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentA.getInstance().myMethod();
}
});
return buttonX;
}
}
Conclusion:
If the TextView is in FragmentB, this code works properly! But I have to hide the TextView which is in FragmentA. So what to do?
Upvotes: 0
Views: 52
Reputation: 1680
So, first of all. Don't save Activities or Fragments in static variables. This might occur it memory leaks.
Then, your GymActivity
and MainActivity
are never showed at same time in UI. What is then purpose to hide some view, that is not visiblle at the moment anymore?
Anyway, to make it working you could make it by using of SharedPreferences
, store some state in your DB or simplest solution - use POJO Singleton object.
public class SomeData {
private static SomeData instance;
private boolean hide;
private SomeData(){
}
public SomeData getIinstance() {
if (instance == null) {
instance = new SomeData();
}
}
public void setHide(boolean hide) {
this.hide = hide;
}
public boolean isHide() {
return hide;
}
}
then you could use it in GymActivity
. replace
FragmentA.getInstance().myMethod();
with
SomeData.getInstance().setHide(true);
As soon your FragmentA
is again visible you could check in onResume
(Fragment lifecycle)
public void onResume() {
super.onResume();
txtview.setVisibility(SomeData.getInstance().isHide() ? View.GONE : View.VISIBLE);
}
Upvotes: 0