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();
}
Related
I have a MainActivity with some cards which have different names. onClick, the title is passed as an intent via the adapter to the secondActivity and displayed as the header. From there, I can go to other activities. If I come back from one of these other activities (via the back button created by establishing second activity as the parent activity) the header is gone. How do I keep the header that was originally passed on as an intent or should I go about this completely different?
I have tried using onResume() and onStart() in the secondActivity to reassign the intent from a global variable.
The adapter class, where the card onClick method is written:
#Override
public void onBindViewHolder(final TripHolder tripHolder, final int position) {
Trip trip = trips.get(position);
tripHolder.setDetails(trip);
tripHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SecondActivity.class);
TextView card_title = v.findViewById(R.id.TripNamecl);
intent.putExtra("card_title", card_title.getText().toString());
context.startActivity(intent);
}
});
}
The secondActivity where the header should be displayed:
public class SecondActivity extends AppCompatActivity {
String name;
TextView header;
static final String STATE_HEADER = "header";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
name = getIntent().getStringExtra("card_title");
header = findViewById(R.id.TripsHeader);
header.setText(name);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(STATE_HEADER, name);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
name = savedInstanceState.getString(STATE_HEADER);
header.setText(name);
}
public void launchMapsActivity(View view) {
Uri gmmIntentUri = Uri.parse("geo:48.8566°,2.3522");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
}
public void launchTravelActivity(View view) {
Intent intent = new Intent(this, TravelActivity.class);
startActivity(intent);
}
public void launchPlansActivity(View view) {
Intent intent = new Intent(this, PlansActivity.class);
startActivity(intent);
}
}
SOLUTION:
The solution was to put android:launchMode="singleTop" into the manifest file for the secondactivity. It's described in more detail here: How can I return to a parent activity correctly?
In order to do it you should override onSavedInstanceState in your SecondActivity.
You can use something like that, obv adapt it to your needs:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//here you can save your variables
savedInstanceState.putString("myHeader", name);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//here you can retrieve your variables
name = savedInstanceState.getString("myHeader");
}
Let me know if this worked! good luck
remove header.setText(name); from onResume and onStart methods
If you want to save data in first activity between lifecicle method calls you have to save your data in Bundle object.
Override method onSaveInstanceState(Bundle bundle) of your activity:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("AStringKey", variableData);
outState.putString("AStringKey2", variableData2);
}
And also override Activity method onRestoreInstanceState(Bundle savedInstanceState):
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
variableData = savedInstanceState.getInt("AStringKey");
variableData2 = savedInstanceState.getString("AStringKey2");
setYourHeaderMethodExample(variableData2)
}
When coming back from third activity to second activity onStart() and onResume() method call executes. Try not using onStart() and onResume() for setting the header and use onCreate method for setting the header.
onCreate() method executes only once in its lifetime, but onStart() and onResume() will execute whenever the activity comes to screen. So when coming back from third activity we don't have any values in the intent.
In your second activity's onCreate method add:
getSupportActionBar().setHomeButtonEnabled(true);
and override the onOptionsItemSelected() method in the class and add the following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You don't need to override the onStart() and onResume() methods.
I am using Android Studio. I have two activities, MainActivity and Main2Activity. There is an edit text and a button in each one. How do I keep the input in the edit text in any activity when I go to the second activity? I tried many answers but nothing worked.
Here is the code of the activity.
public class MainActivity extends AppCompatActivity {
EditText editText2;
Button button;
String var1 ;
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("var1", var1);
editText2 = (EditText)findViewById(R.id.editText2);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
var1 = savedInstanceState.getString("var1");
var1 = editText2.getText().toString();
editText2.setText(var1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Click1(View view)
{
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
}
To carry over values from one activity to another, you need to add a line of code before startActivity(i);. This line of code that you need to add is:
i.putExtra("inputValue", String.valueOf(editText2.getText()));
Then in the other activity, in the onCreate() method, add this:
Intent intent = getIntent();
String inputValue = intent.getStringExtra("inputValue");
editText.setText(inputValue);
Obviously I don't know the name of your EditText in the MainActivity2 so replace the editText with its name.
Hope this helps!
You need to check for the existence of savedInstanceState in your onCreate function.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for a savedInstanceState
if(savedInstanceState != null){
var1 = savedInstanceState.getString("var1");
editText2.setText(var1); // Making sure that you have assigned editText2 already, of course.
}
}
EDIT
Okay really simplified version, let's call this my Activity2
public class test {
String username, id;
TextView view1, view2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view1 = (TextView)findViewById(R.id.textView1);
view2 = (TextView)findViewById(R.id.textView2);
new testFunction().execute();
view1.setText(username);
view2.setText(id);
}
private class testFunction extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... x) {
username = "1";
id = "2";
return null;
}
}
}
Activity1 has a submit button that starts Activity2 (this activity). After Activity2 calls onCreate(), "12" will appear on the screen. When I click back (destroy Activity2 and go back to Activity1), AND THEN go back to Activity2 again, nothing appears on the screen. Why is this?
This is one problem
new testFunction().execute("1", "2").get();
You should not use get(). It blocks the ui thread waiting for the result. And you should never block the ui thread. Use
new testFunction().execute("1", "2");
Declare the variables as instance variables
TextView view1,view2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
view1 = (TextView)findViewById(R.id.textView1);
view2 = (TextView)findViewById(R.id.textView2);
new testFunction().execute("1", "2")
Then in Asynctask onPostExecute update the views
#override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
// update view here
}
finish the actvity when you have pressed back button like
Intent i = new Intent(this,Here is your class name.Class);
startActivity(i);
finish();
by this whenever you will go in second act by press submit button that time on create call
and you can also call your asnyc task in on resume method every time on resume will call.
You have to store the username and id value when closing the fragment.
Have a look into Fragment lifecycle onSaveInstanceState.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(ARG_NAME, username);
outState.putBoolean(ARG_ID, id);
I have add some code lines to your onCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
//get the values from previous avtivity
Bundle bundle=getIntent().getExtras();
username=bundle.getString("username").toString();
id=bundle.getString("id").toString();
String result = new testFunction().execute("1", "2").get();
TextView view1 = (TextView)findViewById(R.id.textView1);
TextView view2 = (TextView)findViewById(R.id.textView2);
view1.setText(username);
view2.setText(id);
}
in order to get the values you must pass the values from previous activity, I guess you have did it correctly, what you missed is, you forgot to get the Intent values and assign them to username,id variables
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.
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);
}