Activity to Activity Communication - android

Activity myActivity = AssumeSomeActivityExists();
Intent openActivity = new Intent();
openActivity.setAction(Intent.ACTION_VIEW);
openActivity.setClass(myActivity,B.class);
myActivity.startActivity(openActivity);
When we do something like above how to make B instance know that it is been called and created by Activity myActivity?

Use extras with your Intent.
Smth like openActivity.putExtra("calledFromA", true)
Then in B:
protected void onCreate(Bundle savedInstanceState) { {
super.onCreate(savedInstanceState);
boolean isCalledFromA = getIntent().getBooleanExtra("calledFromA", false);
}

Related

open SweetAlertDialog in another activity

I have app with to Activities. I want that a SweetAlertDialog will open in the second Activity when I move from the first Activity to the second one.
So my question is how to open it when I move to another activity? Or how I open SweetAlertDialog without clicking on any buttons?
The simplest way to do anything when an Activity starts is to do it in onCreate(). So just open the dialog in the second Activity's onCreate() method.
On your second Activity, just show it on onCreate()
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// call your dialog here
SweetAlertDialog pDialog = new SweetAlertDialog();
sweetDialog.show();
}
}
I want that a SweetAlertDialog will open in the second Activity when I move from the first Activity to the second one.
It's easy, you just need to create the SweetAlertDialog inside your SecondActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
new SweetAlertDialog(this)
.setTitleText("Here's a message!")
.show();
}
in case you don't always need to use the SweetAlertDialog, you can use a flag:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if(extras != null) {
boolean showDialog = extras.getBoolean("showDialog");
if(showDialog) {
new SweetAlertDialog(this)
.setTitleText("Here's a message!")
.show();
}
}
}
where you can start the SecondActivity with:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("showDialog", true);
this.startActivity(intent);
So my question is how to open it when I move to another activity
If you want to always show the SweetAlertDialog whenever you change to the other Activity, you need to create the dialog inside onResume:
#Override
protected void onResume() {
super.onResume();
new SweetAlertDialog(this)
.setTitleText("Here's a message!")
.show();
}

How to check state of app optimally?

I develop android app. It needs save Internet data and login/password pair. I need to check that in every activity. How to do that optimally? My solution:
I create the BaseActivity class:
public class BaseActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkRequirements();
}
private void checkRequirements() {
if (needToCheckInternetConnection())
checkInternetConnection();
//check other requirements
}
private void checkInternetConnection() {
if (!Updater.getInstance(context).checkInternetConnection()) {
Bundle data = new Bundle();
data.putSerializable(SplashScreenActivity.FIELD_ACTION, SplashScreenActivity.Action.SHOW_TEXT);
data.putString(SplashScreenActivity.FIELD_TEXT, getString(R.string.splashScreenInternetNotAvailable));
changeActivity(SplashScreenActivity.class, data);
}
}
protected void changeActivity(Class<?> goTo, Bundle data) {
Intent intent = new Intent(context, goTo);
if (data != null)
intent.putExtras(data);
startActivity(intent);
finish();
}
}
And I extends all activity classes from this.
But finish() not working. So, how to change that architecture or force to work the finish() method?

How to display math solutions on a new activity?

I am building an Android app. How can I get the calculations' solutions to be displayed on a new activity?
Make your calculation in Activity A.
And send them to the other activity using intent.putExtra(...)
Take a look in this guide.
On activity A
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("YourKeyWord", yourVariableWithValue);
startAcvitity(intent);
On activity B
int result;
Bundle extras = getIntent.getExtras();
if (extras =! null) {
result = extras.getInt("YourKeyWord");
}
Activity A:
public class First extends Activity{
int a,b,c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a = 1,b = 2;
c = a + b;
Intent intent = new Intent(this,Second.class);
intent.putExtra("result", c);
startActivity(intent);
}
}
Activity B:
public class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int result = intent.getExtras().getInt("result");
}
}

Android Activity create and start

I'm currently writting an app for Android. I'm trying to figure out the best place to catch on intent when the activity is created for the first time.
public class DisplayAct extends Activity {
private Intent mNewIntent;
private ViewFinderFragment mViewFinderFrag;
public boolean toot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
setContentView(R.layout.activity_display);
if(savedInstanceState != null) {
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
}
}
This is how I get the intent.
I was wondering if grabbing the intent at oncreate is good or it's better to add onStart and add this action inside in term of design perspective.
Thanks
If you app is open, handle it with onNewIntent. If you app is closed, you wanna use your Bundle that comes with your Intent.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState.getString(MY_ACTION)) {
// Do something
}
EDIT:
I set this action as a String to the Intent's Bundle. I performed it with the code below:
Bundle bundle = new Bundle();
bundle.putString(MainActivity.MY_ACTION, "MY_ACTION");
Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(bundle);

How can I activity close in Android?

I have three classActivity created. One is super class and other sub class and third is HomeActivity.
Code for super class is :
public class MyActivity extends Activity {
Button btnHome = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onHomeClick(View view) {
String LOG_TAG = "Akshar";
System.out.println("Hello11111");
btnHome = (Button) view;
Log.v(LOG_TAG, "index=" + btnHome);
}
}
and my subclass code is :
public class ChooseIsland extends MyActivity {
Button btn_home = null;
MyActivity ob1 = new MyActivity();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooseiland);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
ob1.onHomeClick(btn_home);
}
}
Now I want to go on Home page when click so when I write ?
Intent intent = new Intent(this, HomeActivity.class);
There is no close operation as such in android. You should make sure you do not save anything in stack so whenever you are traversing from one activity to other, make sure you use intent flags to clear history or top of stack and then call finish.
finish() will close the current activity and the previous activity will come to foreground.
Generally to we write finish() method to close activity.

Categories

Resources