Button with if condition - java

I am new to Android, and i am trying to make a one button open 2 activities but is not working for me.
for ex:
on Mainacitivity, there is btn_mathematics and btn_physics open the same activity (Main2acitivity) and find btn_semester1 and btn_semester2, each button will open 2 other activities for the semester modules.
If the user on Mainacitivity clicked on:
btn_mathematics ---> btn_semester1---> will have ModulesMAT
and if clicked on the btn_semester1 same button:
btn_physics ---> btn_semester1 ---> will have ModulesPHY .
MainActivity XML:
<Button
android:id="#+id/btn_mathematics"
android:onClick="btn_mathematics"
android:text="#string/btn_mathematics/>
<Button
android:id="#+id/btn_physics"
android:onClick="btn_physics"
android:text="#string/btn_physics"/>
Main2Activity XML:
<Button
android:id="#+id/btn_semester1"
android:onClick="btn_semester1"
android:text="#string/btn_semester1"/>
<Button
android:id="#+id/btn_semester2"
android:onClick="btn_s2"
android:text="#string/btn_semester2"/>
I guess that no need to add xml for ModulesMAT and ModulesPHY, its pretty similar to the the others.
and now the java code:
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_mathematics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
`public void btn_physics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
Main2Activity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void btn_semester1 (View v)
{
Intent i = getIntent();
String id = i.getStringExtra("id");
if(id == "btn_mathematics")
{
i = new Intent(this, ModulesMAT.class);
startActivity(i);
}
else if (id == "btn_physics")
{
i = new Intent(this, ModulesPHY.class);
startActivity(i);
}
}
public void btn_semester2 (View v)
{
Intent i = getIntent();
String id = i.getStringExtra("id");
if(id == "btn_mathematics")
{
i = new Intent(this, ModulesMAT2.class);
startActivity(i);
}
else if (id == "btn_physics")
{
i = new Intent(this, ModulesPHY2.class);
startActivity(i);
}
}

In MainActivity you can pass the id for recognizing into Main2Activity.
MainActivity
public void btn_mathematics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putString("id","Math");
intent.putExtra(bundle);
startActivity(intent);
}
public void btn_physics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putString("id","Physics");
intent.putExtra(bundle);
startActivity(intent);
}
Main2Activity
String id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle bundle = getIntent().getExtras();
id= bundle.getString("id");
}
public void btn_semester1 (View v)
{
if(id == "Math")
{
i = new Intent(this, ModulesMAT2.class);
startActivity(i);
}
else if (id == "Physics")
{
i = new Intent(this, ModulesPHY2.class);
startActivity(i);
}
}
public void btn_semester2 (View v)
{
if(id == "Math")
{
i = new Intent(this, ModulesMAT2.class);
startActivity(i);
}
else if (id == "Physics")
{
i = new Intent(this, ModulesPHY2.class);
startActivity(i);
}
}

Related

Transferring data from the third activity to the first

From the first activity I would like to go to the second, then from the second to the third. In the third activity I would like to enter the name in EditText, then after pressing the button, go to the first activity and at the same time send the information entered in the third activity.
Unfortunately, after pressing the button in the third activity, instead of returning to the first activity, I return to the second activity. Was the first activity killed? What could I do to ensure that the information is correct for the first activity? This is my code:
First:
public class MainActivity extends AppCompatActivity {
TextView textViewInformation;
Button button_GoToSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);
button_GoToSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
String name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}
Second:
public class Second extends AppCompatActivity {
Button button_GoToThird;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button_GoToThird = findViewById(R.id.button2);
button_GoToThird.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Second.this, Third.class);
startActivity(i);
}
});
}
}
Third:
public class Third extends AppCompatActivity {
EditText editText_Data;
Button button_SendData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
just remove finish(); thats it
The reason that it goes to the second activity is because of this:
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish(); //This kills the current activity.
}
You should do:
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
startActivityForResult(i, RESULT_OK, bundle);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
Bundle bundle = i.getExtras();
String name = bundle.getString("name");
textViewInformation.setText(name);
}
}
When you call finish, it just kills the current activity. If you want to go back to the first activity, just start a new activity for the first activity and pass the data in a Bundle.
If you want to have more of a stack concept, you can use Fragments.

Click countries name and save opened Activity, every time open app , show to last selected countries activity, i want to add change the countries

There is a lot of countries in this app including India, Pakistan, UAE, South Africa..etc. When clicking on name of a country, a new activity will be opened. It will be saved by share preference and when we open the app, the last activity will be opened. I created an app like this. I want to add one more thing to this app.
I want to go to countries list view by a button from the opened activity. The user can change the country here and when opening the app, the changed country's activity must be opened. If all countries are opened like this, there must be a option for choosing another country. I hope you will do this for me.
The project code which is done by me is given below.
The share preference will work on it. I want an option to change the country option.
** countries Listview (MainActivity)**
public class MainActivity extends AppCompatActivity {
CardView ind,pak,uae,south;
String clickedCard;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
ind = (CardView) findViewById(R.id.ind);
pak = (CardView) findViewById(R.id.pak);
ind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent CltButton = new Intent(MainActivity.this, India.class);
clickedCard = "Button 1";
CltButton.putExtra("fromMain", clickedCard);
startActivity(CltButton);
}
});
pak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mgButton = new Intent(MainActivity.this,Pak .class);
clickedCard = "Button 2";
mgButton.putExtra("fromMain2", clickedCard);
startActivity(mgButton);
}
});
}
private void checkPreferences() {
////INDIA Preference
prefs = getSharedPreferences("pref", MODE_PRIVATE);
if (prefs.getString("txt", "").equals("") || prefs.getString("lastActivity", "").equals("")) {
} else {
String txt = prefs.getString("txt", "");
String activity = prefs.getString("lastActivity", "");
Intent CltButton = new Intent(MainActivity.this, India.class);
CltButton.putExtra("fromMain", txt);
startActivity(CltButton);
finish();
}
////PAKISTAN Preference
prefs = getSharedPreferences("pref2", MODE_PRIVATE);
if (prefs.getString("txt2", "").equals("") || prefs.getString("lastActivity2", "").equals("")) {
} else {
String txt2 = prefs.getString("txt2", "");
String activity = prefs.getString("lastActivity", "");
Intent mgButton =MainActivity new Intent(MainActivity.this, Pak.class);
mgButton.putExtra("fromMain2", txt2);
startActivity(mgButton);
finish();
}
}
}
India Activity code
public class India extends AppCompatActivity {
String s;
SharedPreferences prefs;
Button buttonind;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_india);
buttonind = (Button) findViewById(R.id.buttonind);
buttonind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mgButton = new Intent(India.this, Main2Activity.class);
startActivity(mgButton);
}
});
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle == null) {
s = "no data received";
} else {
s = bundle.getString("fromMain");
}
}
#Override
protected void onPause() {
super.onPause();
prefs = getSharedPreferences("pref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("txt", s);
editor.putString("lastActivity", getClass().getName());
editor.apply();
}
}
Pak Activity Code
public class Pak extends AppCompatActivity {
String s;
SharedPreferences prefs;
Button buttonpak;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pak);
buttonpak = (Button) findViewById(R.id.buttonpak);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle == null) {
s = "no data received";
} else {
s = bundle.getString("fromMain2");
}
buttonpak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mgButton1 = new Intent(Pak.this, Main2Activity.class);
startActivity(mgButton1);
}
});
}
#Override
protected void onPause() {
super.onPause();
prefs = getSharedPreferences("pref2", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("txt2", s);
editor.putString("lastActivity2", getClass().getName());
editor.apply();
}
}
You are very welcome for this - It took me some time to do and it is working exactly like you want it to.
MainActivity:
public class MainActivity extends AppCompatActivity {
CardView ind,pak,uae,south;
String clickedCard;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
ind = (CardView) findViewById(R.id.ind);
pak = (CardView) findViewById(R.id.pak);
uae = (CardView) findViewById(R.id.uae);
south = (CardView) findViewById(R.id.south);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle!= null) {
SharedPreferences preferences,preferences1,preferences2,preferences3,preferences4;
SharedPreferences.Editor editor,editor1,editor2,editor3,editor4;
preferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
editor = preferences.edit();
editor.clear();
editor.apply();
preferences1 = getSharedPreferences("pref1", Context.MODE_PRIVATE);
editor1 = preferences1.edit();
editor1.clear();
editor1.apply();
preferences2 = getSharedPreferences("pref2", Context.MODE_PRIVATE);
editor2 = preferences2.edit();
editor2.clear();
editor2.apply();
preferences3 = getSharedPreferences("pref3", Context.MODE_PRIVATE);
editor3 = preferences3.edit();
editor3.clear();
editor3.apply();
preferences4 = getSharedPreferences("pref4", Context.MODE_PRIVATE);
editor4 = preferences4.edit();
editor4.clear();
editor4.apply();
} else {
checkPreferences();
}
//checkPreferences();
ind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent CltButton = new Intent(MainActivity.this, India.class);
clickedCard = "Button 1";
CltButton.putExtra("fromMain", clickedCard);
startActivity(CltButton);
finish();
}
});
pak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mgButton = new Intent(MainActivity.this,Pak.class);
clickedCard = "Button 2";
mgButton.putExtra("fromMain2", clickedCard);
startActivity(mgButton);
finish();
}
});
uae.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent keraButton = new Intent(MainActivity.this, Uae.class);
clickedCard = "Button 3";
keraButton.putExtra("fromMain3", clickedCard);
startActivity(keraButton);
finish();
}
});
south.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ButtonactivityIntent = new Intent(MainActivity.this, South.class);
clickedCard = "Button 4";
ButtonactivityIntent.putExtra("fromMain4", clickedCard);
startActivity(ButtonactivityIntent);
finish();
}
});
}
private void checkPreferences() {
////INDIA Preference
prefs = getSharedPreferences("pref", MODE_PRIVATE);
if (prefs.getString("txt", "").equals("") || prefs.getString("lastActivity", "").equals("")) {
} else {
String txt = prefs.getString("txt", "");
String activity = prefs.getString("lastActivity", "");
Intent CltButton = new Intent(MainActivity.this, India.class);
CltButton.putExtra("fromMain", txt);
startActivity(CltButton);
finish();
}
////PAKISTAN Preference
prefs = getSharedPreferences("pref2", MODE_PRIVATE);
if (prefs.getString("txt2", "").equals("") || prefs.getString("lastActivity2", "").equals("")) {
} else {
String txt2 = prefs.getString("txt2", "");
String activity = prefs.getString("lastActivity", "");
Intent mgButton = new Intent(MainActivity.this, Pak.class);
mgButton.putExtra("fromMain2", txt2);
startActivity(mgButton);
finish();
}
////U A E Preference
prefs = getSharedPreferences("pref3", MODE_PRIVATE);
if (prefs.getString("txt3", "").equals("") || prefs.getString("lastActivity3", "").equals("")) {
} else {
String txt2 = prefs.getString("txt3", "");
String activity = prefs.getString("lastActivity", "");
Intent mgButton = new Intent(MainActivity.this, Uae.class);
mgButton.putExtra("fromMain3", txt2);
startActivity(mgButton);
finish();
}
//// SOUTH AFRICA Preference
prefs = getSharedPreferences("pref4", MODE_PRIVATE);
if (prefs.getString("txt4", "").equals("") || prefs.getString("lastActivity4", "").equals("")) {
} else {
String txt2 = prefs.getString("txt4", "");
String activity = prefs.getString("lastActivity", "");
Intent mgButton = new Intent(MainActivity.this, South.class);
mgButton.putExtra("fromMain4", txt2);
startActivity(mgButton);
finish();
}
}
}
India.java:
public class India extends AppCompatActivity {
String s;
SharedPreferences prefs;
Button buttonind;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_india);
buttonind = (Button) findViewById(R.id.buttonind);
buttonind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String test = "";
Intent mgButton2 = new Intent(India.this, MainActivity.class);
mgButton2.putExtra("test", test);
startActivity(mgButton2);
finish();
}
});
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle == null) {
s = "no data received";
} else {
s = bundle.getString("fromMain");
}
}
#Override
protected void onPause() {
super.onPause();
prefs = getSharedPreferences("pref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("txt", s);
editor.putString("lastActivity", getClass().getName());
editor.apply();
}
}
Pakistan.java
public class Pak extends AppCompatActivity {
String s;
SharedPreferences prefs;
Button buttonpak;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pak);
buttonpak = (Button) findViewById(R.id.buttonpak);
buttonpak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String test = "";
Intent mgButton2 = new Intent(Pak.this, MainActivity.class);
mgButton2.putExtra("test", test);
startActivity(mgButton2);
finish();
}
});
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle == null) {
s = "no data received";
} else {
s = bundle.getString("fromMain2");
}
}
#Override
protected void onPause() {
super.onPause();
prefs = getSharedPreferences("pref2", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("txt2", s);
editor.putString("lastActivity2", getClass().getName());
editor.apply();
}
}
You can repeat the process for all the other countries. In MainActivity where I check if (bundle!= null) can be simplified, but it is working.

Unable to get string from a Bundle

I created a new Activity once a Login is successful. But when I start the app, the app crash within 5 seconds with the message
Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
Error is coming from this
name.setText(" "+bundle.getString("name"));
public class LoginActivity extends Activity {
public ImageView bgLogo;
Button login_button;
EditText Username, Password;
String username, password;
String login_url = "http://192.168.0.19/login.php";
AlertDialog.Builder builder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // Enlever la barre bleue
setContentView(R.layout.activity_login);
initExit ();
builder = new AlertDialog.Builder(LoginActivity.this);
login_button = (Button) findViewById(R.id.bLogin);
Username = (EditText) findViewById(R.id.etUsername);
Password = (EditText) findViewById(R.id.etPassword);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
username = Username.getText().toString();
password = Password.getText().toString();
if (username.equals("") || password.equals("")) {
builder.setTitle("Mince une erreur...");
displayAlert("Veuillez entrer un username et un mot de passe correct...");
}
else {
StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
if (code.equals("login_failed")) {
builder.setTitle("Erreur d'authentification");
displayAlert(jsonObject.getString("message"));
}
else {
Intent intent = new Intent (LoginActivity.this, UserAreaActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", jsonObject.getString("name"));
intent.putExtras(bundle);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Erreur", Toast.LENGTH_LONG).show();
error.printStackTrace();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map <String, String> params = new HashMap<String, String>();
params.put("user_name", username);
params.put("password", password);
return params;
}
};
MySingleton.getInstance(LoginActivity.this).addToRequestque(stringRequest);
}
}
});
}
private void initExit() {
bgLogo = (ImageView) findViewById(R.id.bgLogo1);
bgLogo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent (LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
public void displayAlert (String message) {
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Username.setText("");
Password.setText("");
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
#Override
public void onBackPressed() {
// do nothing.
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
return;
}
}
public class UserAreaActivity extends Activity {
public ImageView bgNet;
public ImageView bgChat;
public ImageView bgStats;
public ImageView bgGo;
public Button bLogout;
TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // Enlever la barre bleue
setContentView(R.layout.activity_user_area);
name = (TextView) findViewById(R.id.name);
Bundle bundle = getIntent().getExtras();
name.setText(" "+bundle.getString("name"));
initGoHome ();
initPlay ();
initGoStats ();
initGoChat ();
buttonLogout ();
}
#Override
public void onBackPressed() {
return;
}
private void initGoHome () {
bgNet = (ImageView) findViewById(R.id.bgNet);
bgNet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent (UserAreaActivity.this, HomeActivity.class);
startActivity(intent);
}
});
}
private void initPlay () {
bgGo = (ImageView) findViewById(R.id.bgGo);
bgGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent (UserAreaActivity.this, PlayActivity.class);
startActivity(intent);
}
});
}
private void initGoStats () {
bgStats = (ImageView) findViewById(R.id.bgStats);
bgStats.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent (UserAreaActivity.this, StatsActivity.class);
startActivity(intent);
}
});
}
private void initGoChat () {
bgChat = (ImageView) findViewById(R.id.bgChat);
bgChat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent (UserAreaActivity.this, ChatActivity.class);
startActivity(intent);
}
});
}
private void buttonLogout () {
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent (UserAreaActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
Replace this code snippet:
Bundle bundle = getIntent().getExtras();
name.setText(" "+bundle.getString("name"));
with
Bundle bundle = getIntent().getExtras();
if (bundle != null)
{
name.setText(" "+bundle.getString("name"));
}
Your problem will be solved.
Attempt to invoke virtual method 'java.lang.String
android.os.Bundle.getString(java.lang.String)' on a null object
reference
Well, you need to avoid calling a method on a null object. In your case:
name.setText(" "+bundle.getString("name"));
The bundle is a null object in this case. So it throws an error.
The solution to avoid this crash is to add a if/else statement
if (bundle != null)
// set text for name like what you have done
else
// set a default text for name
But, I suggest it's better to check your code and understand why your bundle is getting null.
The code is resolved for me by typing
in Activity where you get the value by bundle
Bundle bundle = getIntent().getExtras();
hName = bundle.getString("sharedName");
b1 =new Bundle(); //this b1 should be declared Globally where you are getting value
b1.putString("sharedName",hName);
Replace this code snippet:
"class".with(getActivity())
.using(Album.class)
.bucketId(Application.getBucketId())
.entityId(getArguments().getString(YourActivity.GALLERY_ID))
.retrieve(new XXXX()); Change code : Use Bundle method To replace code below type Bundle bundle = getArguments();
"example".with(getActivity())
.using(Album.class)
.bucketId(Application.getBucketId())
.entityId(bundle.getString(YourActivity.GALLERY_ID))
.retrieve(new XXXX());

How to have multiple intents on the same java program

public class FullscreenActivity extends AppCompatActivity {
private ImageButton act;
private ImageButton sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
act = (ImageButton) findViewById(R.id.act);
sat = (ImageButton) findViewById(R.id.sat);
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
}}
What am I doing worng. I have a main UI with six imagebuttons, each one linking to a different activity. How can I link then all in the main activity which is called fullscreen activity
I think you want to bring the FullScreenActivity to front so you only have to finish the front Activity because you didn't finish it
just do finish(); on your sat.class or act.class
or....
You can do this too on your sat.class or act.class:
Intent i = new Intent(this, FullScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
why is the above part inside some other button's click listener? Move it outside the anonymous inner method like
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
}
});
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});

Using Conditions to Pass Data between Activities

Successfully I tried to pass data between three Activities.
That is :
(Data3)Activity1(Data1)-->(Data1)Activity2(Data2)-->(Data2)Activity3)
Now the Problem is:
I want pass to data between these activities using conditions. That
is in Activity2, before sending data to Activity3 I want to check
WORD = "word building"
DROP = "word built"
if WORD FROM EDITTEXT == WORD
pass data to Activity1 AND
goto Activity1
else
if WORD FROM EDITTEXT == DROP
pass data to Activity3 AND
goto Activity3
Here is the code for Activity1 named PickCard.java
public class PickCard extends Activity {
String card = "Card Picked";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
displayIntentData();
findViewById(R.id.sendButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PickCard.this, BuildWord.class);
intent.putExtra("key", card);
startActivity(intent);
}
});
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
Here is the code for Activity2 BuildWord.java
public class BuildWord extends Activity {
String word = "Word Building";
String finished = "Word Built";
EditText simulate = (EditText) findViewById(R.id.dataToSend);
String getdata = simulate.getText().toString();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buildword);
displayIntentData();
if (getdata == word) {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData1);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
Here is the code for Activity3 named DropCard.java
public class DropCard extends Activity {
String drop = "Card Dropped";
String declared = "User Declared";
boolean won = false;
/*Intent intent = getIntent();
TextView tv = (TextView)findViewById(R.id.intentData2);
Bundle extras=intent.getExtras();*/
String get = "word building";//extras.getString("key");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dropcard);
displayIntentData();
if (get == "word built") {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, Declare.class);
intent.putExtra("key", declared);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, MainActivity.class);
intent.putExtra("key", drop);
startActivity(intent);
//notice we dont call finish() here
}
});
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData2);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
use
if (getdata.equals(word)){
}
instead of
if (getdata == word){
}
for comparing Strings.as in your current code replace all == with equals
if (getData.getText().toString().equalsIgnoreCase("String name"))//change this line in your code for checking
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
}
else
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
Its a simple you have to compare in that default method equalsIgnoreCase().
String name = edit_text.getText().toString();
if(name.equalsIgnoreCase("WellCome"))
{
//CALL YOUR ACTIVITY ONE
}
else
{
//CALL YOUR ACTIVITY THREE
}

Categories