Is there any way to Detect userInterations in Android fragments? - android

Could any one help me out with this situation.
I have implemented OnUserInteraction() method for Android Activity it is working fine for me.
But I want it for Fragments too.How can i able call OnUserInteraction() or is there any another way to identify userInteraction with the UI.

#Sunil's answer causes java.lang.StackOverflowError so I corrected it. Below code works smoothly
Create a java class in your app named UserInterationListener and put below code there
public interface UserInteractionListener {
void onUserInteraction();
}
Then create an instance variable in your activity, for this interface as below
private UserInteractionListener userInteractionListener;
Then implement a setter method for this variable, in your activity.
public void setUserInteractionListener(UserInteractionListener userInteractionListener) {
this.userInteractionListener = userInteractionListener;
}
Now override the onUserInteraction method of your activity and if the listener variable is not null, invoke the interface method.
#Override
public void onUserInteraction() {
super.onUserInteraction();
if (userInteractionListener != null)
userInteractionListener.onUserInteraction();
}
Now, in your fragment class, implement UserInteractionListener as below
public myFragment extends Fragment implements UserInteractionListener
also override interface's method
#Override
public void onUserInteraction(){
//TODO://do your work on user interaction
}
then in your fragment invoke your activity's userinteraction setter method like below
((YourActivity) getActivity()).setUserInteractionListener(this);
this last part is important.

There is another way around.
Create a listener in your activity as below
public interface UserInteractionListener {
void onUserInteraction();
}
Then create an instance variable in your activity, for this interface as below
private UserInteractionListener userInteractionListener;
Then implement a setter method for this variable, in your activity. (You can even keep a List of eventlistener objects, if you want to pass same userinteraction to multiple consumers)
public void setUserInteractionListener(UserInteractionListener userInteractionListener) {
this.userInteractionListener = userInteractionListener;
}
Now override the onUserInteraction method of your activity and if the listener variable is not null, invoke the interface method.
#Override
public void onUserInteraction() {
super.onUserInteraction();
if (userInteractionListener != null)
userInteractionListener.onUserInteraction();
}
Now, in your fragment class, register for events as below
((YourActivity) getActivity()).setUserInteractionListener(new YourActivity.UserInteractionListener() {
#Override
public void onUserInteraction() {
// Do whatever you want here, during user interaction
}
});

Related

Android how to put data from fragment to main activity

For Fragment(put data to activity)
m=(MainActivity)getActivity();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent in=new Intent(getActivity(),MainActivity.class)
in.putExtra("test",test);
startActivty(in)
}
},10);
For Activity (get data from fragment )
{
String get_data=getIntent.getStringExtra("test");
}
//it will return always null...any body help me?
startActivty(in) will start the same activity.
Instead of this, you can make use of Interface. It's the easiest way to pass the data.
in your fragment, you can have an interface like,
SubmitData mSubmitData;
public interface SubmitData{
void DataListener(String s);
}
mSubmitData.DataListener("data to be sent");
In your activity, implement the SubmitData interface. It will make you override the DataListener method, where you can get the data.
public class MyActivity extends AppCompatActivity implements YourFragment.SubmitData{
#Override
public void DataListener(String s) {
// Data from the fragment
}
This questions has been asked and answered multiple times. You can find a valid reply here https://stackoverflow.com/a/9977370/5828132
Basically, it consists of creating an interface in the Fragment (for example) including a simple method. The Fragment has to declare a field of that type, and the Activity hosting the Fragment has to implement (implements) that interface. Both entities are usually connected using a explicit cast, in the onAttach() callback of the Fragment life-cycle, i.e.:
#Override
public void onAttach(Context context) {
super.onAttach(context);
// fragmentField_interfaceType = (interfaceType) context;
}
Hope it helps!

How can I Perform networking(save results in the database using volley) based on the result I got from a fragment?

I have an activity and a fragment. If I click a button then a fragment is called. Upon the result I got from the fragment I need some networking call to perform using volley. But I can't do any networking call from activity unless I call this within onClick() method.
I tried to perform networking from within onClick() of the fragment but that did not worked too.
How can i perform networking from the activity upon the result I got from the fragment? Do I must call from within onClick()?
This is the fragment
This is the Activity
I think that you can use callback.
Create intefrace and declare method saveResult() in it.
public interface YourInterface{
void saveResult();
}
After that your Activity must implement this interface and add your code for save result in database in saveResult method body
public class YourActivity implements YourInterface{
#override
void saveResult(){
//your code here
}
}
And finnaly in your fragment call method when you whant or when your fragment is ready.You can call method with help of
callBack.saveResult();
You must override onAttach in your fragment and there must initialize your callback
YourInterface callback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if(activity instanceof YourInterface ) {
callback = (YourInterface ) activity;
}
}
Try with other on click method in at your onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mark_distribution);
assign_buttton=(Button)findViewById(R.id.assign_marks);
view_buttton=(Button)findViewById(R.id.view_marks);
update_buttton=(Button)findViewById(R.id.update_marks);
//Like this-->>>
update_buttton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
marks_type=(String)assigned_marks_type.getText().toString();
marks=Integer.parseInt(assigned_marks.getText().toString().trim());
callback.saveResult(marks_type,marks);
}
});
//********************/
Intent i=getIntent();
course_title= i.getExtras().getString("COURSE_TITLE");
}
Hope to help you!

How to know when activity is paused/resumed from inner view?

From a view (a custom status bar) I need to know when the activity is paused and resumed. Is it possible?
Is it possible?
Yes and very easily.
Declare a interface and provide its implementation via your inner view to the outer activity.
Call the respective delegate in the onResume and onPause of activity to notify your inner view.
Simple. Hope this helps :)
Something like this
class MyActivity extends Activity {
public interface ActivityLifeCycleLister{
onResumeCalled();
}
public ActvityLifeCycleLsistener listener;
public void setActivityLifeCycleListener(ActvityLifeCycleLsistener listener) {
this.listener = listener;
}
#Override
public void onResume(){
super.onResume();
if(listener != null) {
listener.onResumeCalled();
}
}
class MyInnerView extends View {
// Some init method
void init() {
MyActivity.setActivityLifeCycleListener(new ActivityLifeCycleListener() {
void onResumeCalled() {
// Do whatever here
}
});
}
}
}
This approach can be used for all the components of your application who wants to listen life cycle events.
You can just create two methods onResume() and onPause() of your custom View (you can call this two methods whatever you want). Then in your activity you will have override onResume and onPause and call customView.onResume() and customView.onPause().

Callback from (List)Adapter to Activity

I have an Actvivity with a ListView, I set an adapter
MyAdapter extends BaseAdapter
that adapter has a callback interface
OnPdfClickedListener callback;
public interface OnPdfClickedListener {
public void onPdfClicked();
}
and in the Activity
MyActvivity implements MyAdapter.OnPdfClickedListener
and
#Override
public void onPdfClicked() {
Log.d("TEST", "PDF CLICKED in ACTVIIVTY");
}
This is pretty much the same as described here, which works like charm for fragments.
When trying the same from the adapter the callback object is null.
I also tried instantiating like
OnPdfClickedListener callback = new OnPdfClickedListener() {
#Override
public void onPdfClicked() {
// TODO Auto-generated method stub
}
};
I have no errors then but the respective method in the Activity is never called.
My question is 1. why isn't the callback object null when used in a fragment, it's never instantiated
and 2. how can I callback to an Activity from an adapter?
why isn't the callback object null when used in a fragment, it's never instantiated
Your Activity most likely isn't registered as a listener, with the callback variable you created(which has nothing to do with the Activity) an instance of OnPdfClickedListener and (probably) used that when the event happened.
how can I callback to an Activity from an adapter?
Pass a reference to the Activity to your MyAdapter class, cast it to OnPdfClickedListener and use that to call onPdfClicked instead of the current callback variable.
Try adding this:
In your Activity which implements the interface:
new MyAdapter(this,....);
In your Adapter:
MyAdapter(Context context, ...){
callback = (OnPdfClickedListener)context;
}

Android onBackPressed() inside a method

I have an Activity class and inside there are some methods. And I want to implement the onBackPressed() inside the method2 because I have an important variable that I want to free. I can't/don't make this variable with bigger scope and I can't free this variable inside the method2 because I want to terminate the application and the execution of method 2 with the pressing back button.
public class example extends Activity {
public void onCreate(Bundle savedInstanceState) {
method1();
}
public method 1 {
//take some input and assign in a variable.
method2(variable);
}
public method2 {
// do something with the variable that take before at method 1
// and finally press back button
onBackPressed(){}
//free variable , finish ();
}
}
As you know i can't Override the onBackPressed() inside the method only out at the activity area. Can you provide me a solution for this.
You should override the onBackPressed() method in the activity scope and call it from your method.
#Override
public void onBackPressed()
{
super.onBackPressed();
// Do your things.
}
public void method()
{
onBackPressed();
}
If you want to add some complex logic in the onBackPressed method, just create another one with parameters.
public void myOnBackPressed(int param1, String param2)
{
// Do your complex logic.
onBackPressed();
}
public void method()
{
myOnBackPressed(myInt, myString);
}

Categories

Resources