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()
Related
I want to check some condtiton before the main activity starts and based on test result i have to either start a new activity or continue the same activity. How to do this?
you can check it in your OnCreate() method. It is called when your activity start.
onCreate(...){
....
if(want this){
//continue;
}else{
// start new activity
}
}
The default Activity to start is set in the manifest, so a better approach to your problem would be to use fragments. Keep in mind that fragments are faster/lighter, so instead of using Application as a "decision" class to start activities (bad practice), use your main activity. In your onCreate() method, check for your condition and attach the needed fragment.
I am using java annotation to handle this case:
Create the Annoation class
Create CustomContext.java with a startActivity method in it
Create the Interceptor.java
Create a class which implement Interceptor like this (named DemoInterceptor.java here)
Declare a static variable of DemoInterceptor in your activity. and the demo Activity should like this then, start the activity via the custom startActivity in step #2
enjoy and let me know if you have any further question.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(startNewActivity) {
// TODO Fire off intent to start new activity
finish(); // Closes the current activity
return;
}
// TODO Code for current activity.
}
startNewActivity is a boolean indicating whether to launch a new activity or not. It should be assigned a value depending on your condition.
I must write a program with android which can find ssid and show it. My problem is how can i make a relation between an image button in first page and an activity or function in other page.
Buy the way i'm beginner and download the code of searching method because of that i can not recognize which one is the main method or function for pass it to setonclick method that i write for image button in first page?
please answer as soon as you can i need it immediately.
If you are trying to call another activity's method from your existing activity then you should use these steps.
First save the context or instance of the callee activity; for this you can use a class that holds your global data. In this class make the object of this activity.
FirstActivity first = null;
When the callee activity is first created, initialize this instance.
public void setFirstActivity(FirstActivity factivity)
{
first = factivity;
}
And when you do need to call this callee activity's method then access this instance from this global class and with the help of this instance, you will have access to the methods of the callee activity.
public FirstActivity getFirstActivity() {
return first ;
}
use it to get access to the methods or instances of the activity.
Click on the ImageButton, and add the name of the function in the On Click property (Simply the name, like myFunction)
In your code, copy-paste this function and replacemyFunction by the name of the function you chosed
public void myFunction(View v)
{
String ssid = ((EditText)findViewById(R.id.EditTextID)).getText().ToString();
Intent intent = new Intent(this, SecondView.class);
intent.putExtra("SSID_KEY", ssid);
startActivity(intent);
}
And replace SecondView by the class that displays your second page and EditTextName by the ID of your EditText.
In the OnCreate function of your second class, you can get your ssid using the following code
#Override
protected void onCreate(Bundle savedInstanceState) {
String ssid = getIntent().getStringExtra("SSID_KEY");
//Do other work here.
}
Also, on top of the class containing the ImageView, don't forget to add these lines:
import android.view.View;
import android.widget.EditText;
I would like to know, why OnCreate() is called only once at the start of an activity?
Can we call OnCreate() more than once in the same activity?
If yes, than how can we call it? can anyone give an example?
Thanks a lot!!!
Why would you want to called it again? unless the activity is reconstructed, which is called by system. You cannot call OnCreate manually , it is the same reason why you won't call setContentView() twice. as docs:
onCreate(Bundle) is where you initialize your activity. Most
importantly, here you will usually call setContentView(int) with a
layout resource defining your UI, and using findViewById(int) to
retrieve the widgets in that UI that you need to interact with
programmatically.
Once you finish init your widgets Why would you?
UPDATE
I take some words back, you CAN do this manually but I still don't understand why would this be called. Have you tried Fragments ?
Samplecode:
public class MainActivity extends Activity implements OnClickListener {
private Button btPost;
private Bundle state;
private int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
state = savedInstanceState;
btPost = (Button) findViewById(R.id.btPost);
btPost.setOnClickListener(this);
Toast.makeText(getBaseContext(), " " + counter, Toast.LENGTH_LONG)
.show();
}
#Override
public void onClick(View v) {
counter++;
this.onCreate(state);
}
}
onCreate() method performs basic application startup logic that should happen only once for the entire life of the activity .
Once the onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession.
The initialization process consumes lot of resources and to avoid this the activity once created is never completely destroyed but remains non visible to user in background so that once it is bring back to front , reinitialization doesn't happen .
Where you want to call onCreate manually.
Then just do this.
finish();
Intent intent = new Intent(Main.this, Main.class);
startActivity(intent);
finish() calls the current stuff.
And if you are doing somethong getExtra in this activity then do this,
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("key",your_variable);
super.onSaveInstanceState(outState);
}
And add this to your onCreate()
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(savedInstanceState != null)
{
your_variable= savedInstanceState.getString("key");
}
}
Why would you want to call onCreate more than once? You will be re-creating the activity. If this is what you need for whatever reason then finish the activity and use an intent to create a new instance of that activity. Otherwise, you have two instances of the activity at the same time. Hope that helps but if that doesn't make sense then add more information as to what you want so we have context
OnCreate is basically use to create your activity (UI). If you have already created your activity then you need not create it again as you have already created.
It is basically used to initialize your activity and to create user interface of your activity. Activity is a visual part which you can use again and again so.. I think your problem is not to recreate activity but to reinitialize all components of your activity. For that purpose you can create a method initialize_act() and call it from anywhere...
#OnCreate is only for initial creation, and thus should only be called once.
If you have any processing you wish to complete multiple times you should put it elsewhere, perhaps in the #OnResume method.
Recently i realized that onCreate is called on every screen orientation change (landscape/portrait). You should be aware of this while planning your initialization process.
Recreation can be suppressed in AndroidManifest.xml:
<activity
android:configChanges="keyboardHidden|orientation"
android:name=".testActivity"
android:label="#string/app_name"></activity>
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
}
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.
}
}