Access on create variables through out the android activity class - java

I am getting transaction_amount variable from getIntent() in OnCreate method.
String transaction_amount = getIntent().getStringExtra("transaction_amount");
I want to access this variable outside of the OnCreate method.
Something like,
public class PayActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
String transaction_amount = getIntent().getStringExtra("transaction_amount");
}
protected void my_function(){
// I want transaction_amount here.....
}
}

You can use a global variable :
public class PayActivity extends ActionBarActivity {
String value;
#Override
protected void onCreate(Bundle savedInstanceState) {
value = getIntent().getStringExtra("transaction_amount");
}
public void yourFunction() {
// You can acces value here
}
// your activity here....
}

create the transaction_amount variable as a class variable, then initialize it in the onCreate
public class PayActivity extends ActionBarActivity {
private String transaction_amount;
#Override
protected void onCreate(Bundle savedInstanceState) {
transaction_amount = getIntent().getStringExtra("transaction_amount");
}
protected void my_function(){
// I want transaction_amount here.....
}
}

maintain transaction amount as a global variable ,if you require that variable in whole class or if you want acces that variable in method, send that variable as a parameter to that function and called that function from oncreate.

Declare a global variable and intialize it to the transactionamount in onCreate. Then use that global variable anywhere.
public class PayActivity extends ActionBarActivity {
String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
s = getIntent().getStringExtra("transaction_amount");
}
void anyFunction() {
// use the string s here now
}
}

Related

how to use activity context from other activity?

I want to use activity context from another activity simple code example below Any idea ?
public class Activity_A extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_A);
}
}
public class Activity_B extends AppCompatActivity {
Dialog dialog1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_B);
dialog1 = new Dialog(I want Activity_A Context) ; // Is this possible ??
}
}
Thanks
Tamim you can definitely achieve this using a different class by making a public static function in that other class
public class Utils {
public static void showDialog(Context context){
//// your code here
}
now you use it wherever you want...
Utils.showDialog(this);

How to pass context to Intent in android library?

I created a library for the recurrent screen in android and when I tried to implement it in my activity I got this error message.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
at com.expert.recur.ScreenReco.<init>(ScreenReco.java:15)
at com.expert.recurringscreen.MainActivity.onCreate(MainActivity.java:18)
My code.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ScreenReco screenReco;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenReco=new ScreenReco(MainActivity.this);//line 18
screenReco.value = 1000;
screenReco.runnable.run();
}
}
My library:
public class ScreenReco {
Activity activity;
public ScreenReco(Activity activity) {
this.activity = activity;
}
public Context context = activity.getApplicationContext();//line 15
public int value;
public Handler handler = new Handler();
public Runnable runnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(context, context.getClass());
handler.postDelayed((Runnable) context,value);
context.startActivity(i);
}
};
}
You are supposed to create an object of ScreenReco class before assigning values to its variables ...
public class MainActivity extends AppCompatActivity {
ScreenReco screenReco;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenReco=new ScreenReco(); // you are missing this
screenReco.context = this;
screenReco.value = 1000;
screenReco.runnable.run();
}
}
But I strongly recommend you to use constructors for this ... it's the good practice
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.Context android.app.Activity.getApplicationContext()'
on a null object reference
You can create a Constructor.
A constructor is a special method that is called whenever an object is
created using the new keyword.
public class ScreenReco {
Activity activity;
ScreenReco(Activity ctx)
{
this.activity=ctx
}
}
Then
ScreenReco screenReco=new ScreenReco(MainActivity.this);
Or
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenReco=new ScreenReco(MainActivity.this);
Your MainActivity is almost fine. But, it is not recommended to call run(); method of Runnable object ourself. Instead, pass the Runnable object in a constructor of a Thread object and call start(); method on the Thread object and let the system call the run(); method itself when it is appropriate. Your improved MainActivity may look like this :
public class MainActivity extends AppCompatActivity {
ScreenReco screenReco;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenReco=new ScreenReco(MainActivity.this, 1000);//line 18
// screenReco.value = 1000;
// if you want to run `runnable` in a new Thread
// new Thread(screenReco.runnable).start();
//or if you want to run it in the same thread
//new Handler().post(screenReco.runnable);
//screenReco.runnable.run();
}
}
Now moving into your library file i.e. ScreenReco.java, it looks like activity variable hasn't been initialized while you are trying to call getApplicationContext() method in the class as #PraveenSP's answer has already said. A better practice would be declare all required class variable at once and then initialize them all in the class constructor as:
public class ScreenReco {
// if you need this activity variable to just use as context
// then do not use this variable here just use context only
Activity activity;
// the better practice here is to declare these variable as private
// and pass values for these variables in constructor and initialize them there
public Context context;
public int value;
public Handler handler;
public Runnable runnable;
// constructor improved from your code
public ScreenReco(Activity activity, int value) {
this.activity = activity;// this only if you are using activity
// object to something otherwise get rid of this variable.
this.context = activity.getApplicationContext();
this.handler = new Handler();
this.value = value;
this.runnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(context, context.getClass());
// handler.postDelayed((Runnable)context, value);
context.startActivity(i);
}
// this is the edit you need
this.handler.postDelayed(runnable, value);
};
}
// this version of constructor is what exactly I would have done by declaring class variable as `private` up there
// public ScreenReco(Context context, int value) {
// this.activity = activity;
// this.context = context;
// this.handler = new Handler();
// also initialize `value` and `runnable` object here
// this.value = value;
// this.runnable = new Runnable() {
// #Override
// public void run() {
// Intent i = new Intent(context, context.getClass());
// handler.postDelayed((Runnable) context,value);
// context.startActivity(i);
// }
// };
// }
}
Also, I'm so confused by the code you wrote inside the runnable's run() method. It looks like you are trying to start the MainActivity from MainActivity. And, in the line below, you tried to cast context variable to Runnable which is very error-prone.

How to define an Edit_Text to use it in all the java code

Guys I would like to define an Edit_Text in the Main_Activity class to use it in all the java code like in any method I used the types public final,public,private, final but it doesn't work. what is the type that I should do?
public class MainActivity extends Activity
{
// Declare it here
EditText edt;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Reference it here
edt = (EditText) findViewById(R.id.edt);
}
private void someName() {
// You can use it here
edt.setText("Hello");
}
private void otherName() {
// You can also use it here and eveywhere in the whole class
edt.setText("Hello");
}
}
example.class:
EditText et;
public void onCreate(..){
.....
et = findViewbyId(R.id.editText);
// To get the value of et
String str = et.getText().toString().trim();
}

Calling a method of MainActivity from other class

I am developing an Android app and thus, I have a MainActivity class. Inside of that MainActivity class, I have a method, let's call it doSomething():
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doSomething(){
// bla bla bla
}
}
I also have a different class (with different layout) that is called OtherActivity. I want to use the doSomething method inside it:
public class OtherActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
}
}
I tried this:
public class OtherActivity extends AppCompatActivity {
MainActivity main;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
MainActivity main = new MainActivity();
main.doSomething();
}
}
But it does not work. I also tried to make OtherActivity to extend the MainActivity, doing the following:
public class OtherActivity extends MainActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
// Let's use doSomething()
super.doSomething();
}
}
But it does not allow me to initialize the layout...
How can I do?
Thanks in advance.
To communicate between to Activity Broadcast is the best way, and for the same application, we can use local broadcast using LocalBroadcastManager.
First, we should register one broadcast in MainActivity,
public class MainActivity1 extends AppCompatActivity {
public static final String INTENT_FILTER = "do_some_action";
public static final String INTENT_BUNDLE_VALUE = "value1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
LocalBroadcastManager.getInstance(this).registerReceiver(
mChangeListener, new IntentFilter(INTENT_FILTER));
}
#Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mChangeListener);
}
private BroadcastReceiver mChangeListener = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intentData) {
// you can do anything here
if (intentData != null && intentData.hasExtra(INTENT_BUNDLE_VALUE)) {
String value = intentData.getStringExtra(INTENT_BUNDLE_VALUE);
doSomeAction(value);
}
}
};
private void doSomeAction(String value) {
}
}
Then to do some action in MainActivity from OtherActivity, we can send Local broadcast from OtherActivity it will reach the receiver of Which we register in MainActivity,
public class OtherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
// You can call MainActivity to do some actions
Intent intent = new Intent(MainActivity1.INTENT_FILTER);
intent.putExtra(MainActivity1.INTENT_BUNDLE_VALUE, "Any string or any value");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Done!!!.
Something like this should do the trick, I'm going to make a static navigator to handle your navigation logic. If you are opposed to static methods you could also make them on your Application object to make it easier to manage dependencies, I'm just making it static for simplicity.
//Making this fully static for simplicity, this is fine for a small app
//you can make it a singleton on the application class for more flexibility
public class Navigator {
//static member vars that determine navigation
// pass in Context if needed for navigation purposes
public static void doSomething(Context context){
// bla bla bla
}
}
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
}
private void doSomething() {
Navigator.doSomething(this);
}
}
public class OtherActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity_layout);
}
private void doSomething() {
Navigator.doSomething(this);
}
}

Cannot access member object inside activity

Why can't I access remTime object? I get runtime exception while I can access all other Activity members declared the same way.
public class MainActivity extends Activity {
ListView lstDayInterval, lstWeekDay, lstMonthDay, lstMonth;
...
RemCalendar remTime;
#Override
public void onCreate(Bundle savedInstanceState) {
...
remTime.setToNow();
}
public class RemCalendar {
private Calendar C;
public RemCalendar() {
C = Calendar.getInstance();
}
public void setToNow() {
C = Calendar.getInstance();
}
}
Thanks
you forget to initialize remTime instance before calling method from RemCalendar class. initialize it inside onCreate :
#Override
public void onCreate(Bundle savedInstanceState) {
...
remTime=new RemCalendar();
remTime.setToNow();
}

Categories