I want to allow users to save a list of favourite items from a list, so I display the full list using a Listview with checkboxes, and the user will check or uncheck items in the list.
When the user presses the back button, I want to be able to save the checked items out to a separate file on the SD card.
I'm using the following method in my Activity:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
However, the list that I create within my OnCreate method is not available within the onBackPressed method - ie it's out of scope.
If I try and pass the list into the onBackPressed method as follows:
#Override
public void onBackPressed(ArrayList<HashMap<String, Object>> checked_list) {
// TODO Auto-generated method stub
}
Then I get the error:
"The method onBackPressed(ArrayList>) of type SetFavourites must override or implement a supertype method" and Eclipse prompts to remove the #Override.
But when I do this, then the onBackPressed method never gets called.
How can I pass variables into the onBackPressed method so that I can perform actions on data within my Activity before exiting?
Thanks
You can define a variable in the class' scope, ie outside onCreate().
class Example {
int mVisible = 0;
void onCreate() {
int notVisible = mVisible;
}
void onBackPressed() {
mVisible = 10;
}
}
You can't use #Override to override a non-existent method. In other words onBackPressed(ArrayList<...> foo) is not an existing method of the Activity class.
To access your list declare it as an instance member of your Activity...
public class MyActivity extends Activity {
ArrayList<HashMap<String, Object>> checked_list;
// onCreate(...) here
// onBackPressed() here
}
Related
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
}
});
The main activity of the application is to display a list. The user clicks on something on the list which opens a edit screen. Upon fisnish, the edit screen is closed - and I want the original list to be updated with whatever hapenned on the edit screen. I save the data to a file - and I can just read it again to update the list. However I don't know where to insert the re-read code.
In the ListActivity - what method is called whe the list gets focus again?
This is my main List activity code:
Creating the view:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate our UI from its XML layout description.
setContentView(R.layout.my_activity);
list=new Data_List(this); // my data reading class
list.read_data(); // reads from a file
load_dynamic_list();
}
Loading the data:
private void load_dynamic_list(){
ladapter=new
list_adapter(this,android.R.layout.simple_list_item_1,list); // the type is actually ignored // getview function in list_adapter handles everything
setListAdapter(ladapter);
this.getListView().invalidate();
}
Something was selected:
protected void onListItemClick (ListView l, View v, int position, long id){
int a;
intent = new Intent(this,Editing.class);
intent.putExtra("New_entry",0);
intent.putExtra("Entry",position);
//start the second Activity
this.startActivity(intent);
}
In the Editing function I end off like this:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.button_save){
do_save(); // saves to a file
// I want something like: caller.getListView().invalidate();
finish();
}
if(v.getId() == R.id.button_cancel){
finish();
}
}
What method can I override or call that will execute when the editing is done? At that point I want to read_data() and then load_dynamic_list() again.
You have to use AsyncTask.
Prefer URL :
http://steveliles.github.com/android_s_asynctask.html
http://www.vogella.com/articles/AndroidPerformance/article.html
With example :
http://labs.makemachine.net/2010/05/android-asynctask-example/
#Override
protected void onResume() {
super.onResume();
list=new Data_List(this);
list.read_data();
load_dynamic_list(); // becuase whatever was edited needs to be reread.
}
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);
}
i have a tabActivity that hold 3 tabs.
from one tab i want to open another tab and run a method that refresh the data.
i use this method to switch tabs
public void switchTabInActivity(int indexTabToSwitchTo) {
MyTabsActivity ParentActivity;
ParentActivity = (MyTabsActivity) this.getParent();
ParentActivity.switchTab(indexTabToSwitchTo);
}
to open the tab but i cant' call the method.
any ideas?
According to me, I believe what you are doing here is correct, but still you are not doing the entire flow. Let me explain,
Calling the above method will redirect you to that particular tab. But what you actually have to do is to execute some method in that class. But were are you calling that method.
Consider a Activity with onCreate(),
you could have called that method in your onCreate(). But now when you execute your
public void switchTabInActivity(int indexTabToSwitchTo) {
MyTabsActivity ParentActivity;
ParentActivity = (MyTabsActivity) this.getParent();
ParentActivity.switchTab(indexTabToSwitchTo);
}
method, this will call the onResume() of that activity. So my suggestion would be to override the onResume method of your particular activity which has that method..
you can simply create a static method which can be easily call by using ClassName.methodName();
see example,
public class myAnotherClass
{
public static void accessFromAnotherClass()
{
System.out.println ( "I am accessed publically" );
}
}
// Now Accessing above class method from another class file
public class myFirstClass
{
private void myClassMethod()
{
myAnotherClass.accessFromAnotherClass(); // called from another class. in your case , another tab.
}
}
Is it possible to call a particular method that's in an Activity from a widget?
This is the method I would like to call:
/*
* Close out this screen.
*/
private void finishThisActivity() {
this.finish();
} // End method finishThisActivity.
If this can be done, can you show some sample code?
i will like to share my idea that if you want to call your own method finishThisActivity() from widget then you need to use a tag called "android:onclick="methodname".For you the method name should be "finishThisActivity".
EXAMPLE:
Suppose you want to call this method in place of onClick() in case of Button then you need to use the above tag for button and you need to put the corresponding method
private void finishThisActivity() {
this.finish();
}
outside of onCreate()