Android Shared Preferences not working when relaunch app - java

I have 4 activities: MainActivity, p1, p2, p3.
My app works fine but problem here is that when app force stop or flick up app in home button to close, when the app is opened again, seems shared performance is cleared and my resume button just exit from app .
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
final Button resume = (Button) findViewById(R.id.resume);
Button next = (Button) findViewById(R.id.next);
Button exit = (Button) findViewById(R.id.exit);
resume.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("my_first_time", true)) {
resume.setEnabled(false);
Log.d("Comments", "First time");
settings.edit().putBoolean("my_first_time", false).commit();
}else
{
MainActivity.this.finish();
}
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, p1.class);
startActivity(intent);
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
Xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="resume"
android:layout_width="wrap_content"
android:id="#+id/resume"
android:layout_height="wrap_content" />
<Button
android:text="next"
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"/>
</LinearLayout>
p1:
public class p1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p1);
Button next = (Button) findViewById(R.id.next);
Button home=(Button)findViewById(R.id.home);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(p1.this, p2.class);
startActivity(intent);
}
});
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(p1.this, MainActivity.class);
startActivity(intent);
}
});
}
private void storeCurrentActivity(){
SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=myPref.edit();
editor.putString("lastactivity", p1.this.getClass().getSimpleName());
editor.commit();
}
#Override
public void onResume(){
super.onResume();
storeCurrentActivity();
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="page 1"/>
<Button
android:text="go in main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home"/>
</LinearLayout>
and p2, p3 like p1.

I have experiences in non-shared only but 0 may is a wrong flag!
getSharedPreferences(PREFS_NAME, 0); <-- Please use the Constant Context.MODE_WORLD_READABLE

Analysis:
once you have pressed the resume button it is stored as setting["MyPrefsFile", "my_first_time"] = true and resume-button is disabled in current activity instance.
when the activity gets destroyed and is recreated the resumebutton is not initialized from the settings so it is enabled.
to fix add this to you onCreate
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0)
resume.setEnabled(settings.getBoolean("my_first_time", true));

Related

I can't start an Activitiy by clicking a Button

i'm trying to start an Activitiy by clicking a Button. If the user clicks the PLAY button in the MenuActivity the GameActivity should be started. But it doesn't work.
public class MenuActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
public void startGameEngine(View view) {
Intent myintent = new Intent(this, GameActivity.class);
startActivity(myintent);
}
Here is the GameActivity class which should be started when the user click on the PLAY-Button.
public class GameActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Intent intent = getIntent();
}
Here is the menu.xml file which includes the button
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/menubackground_color"
tools:context="com.example.vincenzoauricchio.example.MenuActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="381dp"
android:layout_height="67dp"
android:layout_marginBottom="440dp"
android:layout_marginTop="88dp"
android:fontFamily="#font/vt323"
android:text="#string/logo2"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#android:color/black"
android:textSize="36sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/btnPlay"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginBottom="265dp"
android:layout_marginTop="87dp"
android:fontFamily="#font/vt323"
android:onClick="startGameEngine"
android:text="#string/button_play"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.0" />
Try implementing the on click listner :
public class MenuActivity extends AppCompatActivity implements View.OnClickListener
Try below code snippet :
Button btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(MainActivity.this);
#Override
public void onClick(View view)
{
if(view == btnPlay)
{
Intent intent = new Intent(MenuActivity.this, GameActivity.class);
startActivity(intent);
}
}
Add your GameActivity to manifest.
Try this.
public class MenuActivity extends AppCompatActivity implements View.OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button btnPlay= (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btnPlay:
Intent myintent = new Intent(MenuActivity.this, GameActivity.class);
startActivity(myintent);
break;
}
}
}
Check your Activity in your Manifestfile.xml or not
Add this to your AndroidManifest.xml
<activity android:name=".GameActivity"/>
The clickOn on the layouts.xml are not always a good practice but they work fine.
Instead of setting your on click listener in the xml, set it programatically in the activities on create.
btnPlay.setOnClickListener(new onClickListener{
void onCLick(){
startGameEngine();
}});
and remove View view from startGameEngine()'s input value.
and make sure you add the GameActivity in the manifest.xml

How to change text from another activity?

I am creating an Android app - using Android Studio.
The app launches in MainActivity.java which fetches activity_main.xml . On activity_main, the user can select one of 3 buttons. No matter which button they select, it will take them to the SAME layout - primary_layout.xml and the Java class associated with that is PrimaryClass.java .
I have a placeholder in primary_layout. I want this placeholder (id: placeholder) to change according to what button was previously selected.
Eg. If Button 1 (id: button1) is clicked, then the placeholder must say “Button 1 was clicked.”
Or let’s say Button 2 (id: button2) was clicked, then the placeholder must say “Button 2 was clicked”. And the same goes for Button 3.
I have created an intent to open PrimaryClass but I'm not too sure how to code the intent for when the button is clicked. I just don’t know how to change the text of the placeholder, depending on which button the user has clicked. I’ve tried using an ‘if statement’ but it doesn’t seem to work.
Or instead of creating another activity, should I rather create a fragment, and if I should, how would I code the fragment in this particular app?
I have attached images and code to better understand.
And here is my code of my classes and layout files:
MainActivity:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.msp.exampleapplication.MainActivity">
<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button1"
android:onClick="clickedButton1" />
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp"
android:id="#+id/button2"
android:onClick="clickedButton2" />
<Button
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:id="#+id/button3"
android:onClick="clickedButton3" />
</RelativeLayout>
PrimaryClass:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
}
}
primary_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="{holder}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:id="#+id/placeholder" />
</LinearLayout>
Your first Activity must look like this:
package com.msp.exampleapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 1 selected");
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 2 selected");
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this, PrimaryClass.class);
intent.putExtra("message","Button 3 selected");
startActivity(intent);
}
});
}
}
Code the second activity as below:
package com.msp.exampleapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class PrimaryClass extends AppCompatActivity {
TextView placeholder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
placeholder=(TextView) findViewById(R.id.placeholder);
placeholder.setText(getIntent().getStringExtra("message"));
}
}
Here is solution of your problem. You have to use Intent to make action in another activity.
Code for MainActivity, create a intent on each button click like below
button_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 1 Clicked");
startActivity(intent);
}
});
Same for button 2 and 3
button_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 2 Clicked");
startActivity(intent);
}
});
button_3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrimaryClass.class);
intent.putExtra("button_text", "Button 3 Clicked");
startActivity(intent);
}
});
Now in PrimaryClass use this code inside onCreate Method
String btn_text;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
btn_text = bundle.getString("button_text");
}
Now set text on lable
textView.setText(btn_text);
Hope you understand this, if not let me know, I'll help you.

Can I open a seperate XML layout with a OnLongClick

I am currently experimenting with layouts and opening new pages, I would like to open one page with a OnLongClickListener but a toast with a OnClickListener using the same text view box.
This is what I've designed so far. It won't work as the long click needs to output a boolean. Any Ideas?
XML main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.how_to_use.MainActivity"
android:id="#+id/main_view">
<TextView
android:id="#+id/long_press_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Long Press To Open \n'Drag n Drop'"/>
</LinearLayout>
XML 2nd page
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/swirl_img"
android:src="#drawable/Swirl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private LinearLayout mainView;
private TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.long_press_textView);
addListenerLongPressBtn();
addListenerOnClickBtn();
}
public void addListenerOnClickBtn() {
txtView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show();
}
});
}
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(this, DragDropActivity.class);
startActivity(intent);
return true;
}
});
}
}
I found a solution to my own problem.
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(view.getContext(), DragDropActivity.class);
startActivity(intent);
return true;
}
});
}

Handle Toggle States in onResume() using Intent and SharedPreferences

I have written a program in which i am using Timer and controlling that timer using Toggle states,
Like: My Toggle's default state is OFF, once i make changes in toggle state from OFF to ON TIMER starts, and when i again change to OFF it stops the timer as per requirement.
But problem starts when my Timer is ON and I switch to other activity and then again come back to Toggle activity and then do changes in toggle state from ON to OFF - it still runs Timer...
ToggleActivity.java:
public class ToggleActivity extends Activity implements OnCheckedChangeListener {
ToggleButton toggleButton;
TextView text;
Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
Button btnSwitchActivity;
boolean toggleState;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toggle);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
text = (TextView) findViewById(R.id.textView1);
btnSwitchActivity = (Button) findViewById(R.id.btnSwitchActivity);
sharedPreferences = getApplicationContext().getSharedPreferences("toggleState",0);
toggleButton.setOnCheckedChangeListener(this);
btnSwitchActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intentSwitchActivity = new Intent(ToggleActivity.this, SwitchActivity.class);
startActivity(intentSwitchActivity);
}
});
}
#Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked)
{
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleState", true);
editor.commit();
text.setText("ON");
startTimer();
} else
{
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleState", false);
editor.commit();
text.setText("OFF");
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
public void startTimer() {
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 1000, 5000);
}
public void stoptimertask(View v) {
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
final String strDate = simpleDateFormat.format(calendar.getTime());
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), strDate, duration);
toast.show();
}
});
}
};
}
public void onResume() {
super.onResume();
toggleState = sharedPreferences.getBoolean("toggleState", false);
Log.v("toggleState-onResume()", Boolean.toString(toggleState));
if (toggleState) {
toggleButton.setChecked(true);
} else {
toggleButton.setChecked(false);
}
}
}
SwitchActivity.java:
public class SwitchActivity extends Activity {
Button btnToggleActivity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch);
btnToggleActivity = (Button) findViewById(R.id.btnToggleActivity);
btnToggleActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(SwitchActivity.this, ToggleActivity.class);
startActivity(intent);
/**
* if i use finish() instead of Intent to switch to ToggleActivity
* my Timer works fine
*/
// finish
}
});
}
}
activity_toggle.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:gravity="center"
android:background="#ffffff"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".ToggleActivity" >
<ToggleButton
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:checked="false"
android:text=""
android:textOff=""
android:textOn="" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="#string/string_toggle_off"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnSwitchActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/string_btn_switch"/>
</LinearLayout>
activity_switch.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#ffffff"
android:orientation="vertical" >
<Button
android:id="#+id/btnToggleActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/string_btn_goback"
/>
</LinearLayout>
You should probably save the state of the toggle button in the savedInstanceState bundle by overriding onSaveInstanceState.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outstate.putBoolean("KEY", yourButton.isToggle())
}
And then you can access it by reading the savedInstanceState bundle given to you in onCreate of your activity or onCreateView/onViewCreated if you work in a fragment.
Hope this helps.

visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android

everyone. I am new in android. I am creating two activities in android ,1st activity has two image view and one button . and 2nd activity has one button. when I go to second activity and then from second activity agian . one image view of 1st activity should visibility gone. how can i do this.
here is my code
Activitymain.xml
<ImageView
android:id="#+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
}
secondactivity.xml
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
please help me
Activitymain.xml
<ImageView
android:id="#+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getBoolean("isResult", false)){
imageView1.setVisability(View.GONE);
}
}
}
secondactivity.xml
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondIntent.putExtra("isResult", true);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
Use putextra(tag, some variable to use in mainActivity) and Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag) to pass a variable (probably a boolean) to tell the onCreate() to show or not show the pic.
EDIT:
Here is one way to implement it: in the first activity:
1) Check if there are extras (A.K.A. if second activity was loaded/ there is a true)
2) If (yes) { if (first pic is loaded) { remove first pic } else if (first pick is not but second is loaded) { remove second pic } ... for all the pictures you want to do this to.
Remember you only need to pass true/false from the second activity (and then handle it in the first/one being modified)

Categories