The code was not function if SharedPreferences put in there - java

Hy guys i have a problem with my code, if i put SharedPreferences my code not function
i will explain with my following code
this is menu.class
public class menu extends Activity {
Button f1, f2;
ImageView f2lock;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.famouslevel);
f1 =(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level1 = new Intent ();
level1.setClassName ("com.example.game", "com.example.game.levelone");
startActivityForResult (level1, 0);
}
});
}
public void onActivityResult (int requestCode, int resultCode, Intent level1){
super.onActivityResult (requestCode, resultCode, level1);
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
switch (resultCode) {
case 2: f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
f2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level2 = new Intent ();
level2.setClassName ("com.example.game", "com.example.game.leveltwo");
startActivityForResult (level2, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splashscreen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
if i use this code
switch (resultCode) {
case 2: f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
The code was running perfect, the f2 button in menu.xml is show up to VISIBLE and f2lock GONE but of course without SharedPreferences it won't save.
So if I change the code and put SharedPreferences like this:
switch (resultCode) {
case 2:
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();
if(levelTwoUnlocked){
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
else {
f2.setVisibility(View.GONE);
f2lock.setVisibility(View.VISIBLE);
}
}
The f2 button in menu.xml does not turn VISIBLE, it is still GONE. Yhe code does not function to make f2 button VISIBLE and f2lock GONE.
Can anyone help me with this code?
UPDATED
i have changed my code again
switch (resultCode) {
case 2:
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
//to make f2 VISIBLE and f2lock GONE
boolean levelTwoUnlocked = preferences.getBoolean("f2", true);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();
if(levelTwoUnlocked){
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
else {
f2.setVisibility(View.GONE);
f2lock.setVisibility(View.VISIBLE);
}
}
still have the same problem, f2 won't setVisibility(View.VISIBLE)

Actually I don't understand well what you're trying to do, but mistake is you never toggle your boolean levelTwoUnlocked. So don't mind how many times you enter in the if, the route will be always the same (I bet levelTwoUnlocked=false because is default Java boolean value):
if(levelTwoUnlocked){
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
levelTwoUnlocked = false;
} else {
f2.setVisibility(View.GONE);
f2lock.setVisibility(View.VISIBLE);
levelTwoUnlocked = true;
}

Related

How to save the data in my ListView from the first Activity to the second Activity?

I am writing an app for a restaurant and would like to be able to select a specific dish that you would like to order and that it has been added to OrderActivity where, in the form of ListView, you will be displaying individual dishes selected by the user.
I do not know how to do it in the best way, do you need to use the interface and maybe just get the intention of a specific dish?
And how do I save a specific request in OrderActivity so that when I return to an earlier Activity I do not lose the saved data in the ListView?
I managed to solve the problem of transmitting data from one Activity to the second Activity and showing it on the ListView, I do not know how to save that data, say on the example of SharedPreferences?
If I click the back button in my second Activity, my list becomes empty.
I understand that the fault is on onResume() because I am killing the second Activity, when I come back to the first, is that so?
How to solve the problem?
FirstActivity:
public class DinnerDetailActivity extends AppCompatActivity {
public static final String EXTRA_DINNER = "dinner";
private final int requestCode = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_obiady_domowe_detail);
int dinner = (Integer) getIntent().getExtras().get(EXTRA_DINNER);
String dinnerName = Dinner.dinn[dinner].getName();
TextView textView = (TextView) findViewById(R.id.dinner_text);
textView.setText(dinnerName);
int dinnerImage = Dinner.dinn[dinner].getImageResourceId();
ImageView imageView = (ImageView) findViewById(R.id.dinner_image);
imageView.setImageDrawable(getResources().getDrawable(dinnerImage));
imageView.setContentDescription(dinnerName);
Toolbar myChildToolbar = (Toolbar)
findViewById(R.id.my_child_toolbar_obiady_detail);
setSupportActionBar(myChildToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
/* #Override
protected void onResume() {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("KEY", listItems.add();
editor.apply();
Toast.makeText(getApplicationContext(), "Save!", Toast.LENGTH_SHORT).show();
super.onResume();
}
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
TextView textView = (TextView) findViewById(R.id.dinner_text);
CharSequence dinnerName = textView.getText();
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, dinnerName);
shareActionProvider.setShareIntent(intent);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create_order:
Intent intent = new Intent(this, TopFragment.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Click button, and add dinnerName to SecondActivity ListView
public void addInOrder(View view) {
int dinner = (Integer) getIntent().getExtras().get(EXTRA_DINNER);
String dinnerName = Dinner.dinn[dinner].getName();
Intent intent1 = new Intent(this, CreateYourOrderActivity.class);
intent1.putExtra("OK", dinnerName);
startActivityForResult(intent1, requestCode);
}
}
Second Activity:
public class CreateYourOrderActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> listItems;
private String dinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zloz_zamowienie);
Toolbar myChildToolbar = (Toolbar)
findViewById(R.id.my_child_toolbar);
setSupportActionBar(myChildToolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
textView = (TextView) findViewById(R.id.text_view);
}
/*
#Override
protected void onResume() {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("KEY", obiad);
editor.apply();
listItems.add(obiad);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Save!", Toast.LENGTH_SHORT).show();
super.onResume();
}
*/
/* #Override
public void onBackPressed() {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("KEY", listItems.get(0).toString());
editor.apply();
Toast.makeText(getApplicationContext(), "Save!", Toast.LENGTH_SHORT).show();
}*/
public void saveInfo(View view) {
}
public void openInfo(View view) {
SharedPreferences sharedPref = getSharedPreferences("KEY", Context.MODE_PRIVATE);
String obiadNames = sharedPref.getString("KEY", "");
textView.setText(obiadNames);
//listItems.add(obiadNames);
//adapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create_order:
Intent intent = new Intent(this, MainActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Intent intent = getIntent();
if(resultCode == RESULT_OK) {
if(requestCode == 1){
dinner = data.getExtras().getString("OK");
listView = (ListView) findViewById(R.id.listView);
listItems.add(dinner);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
// adapter.add(dinner);
adapter.notifyDataSetChanged();
finish();
}
}
}
So as you can see in the attached photos we have the first Activity, several dishes from which we go to the detailed Activity, where we have AddOrder Button.
I would like to click on this button to add the name of my specific dish in the 3 Activities that you see in the pictures.
This is to be added as a ListView.
Also, I would like to have the names of dishes not gone when I return to 1 Activity.
SharedPreferences are for simple values.
You'll need a proper database (Sqlite, Realm, or other) with which you can persistently store and query your data across the entire application without passing entire objects across Intent boundaries.
More specifically, you need to replace the Dinner.dinn array
To get specific items when you go to a detail view, you can pass the database ID of the object, then query it later.
When you add a new item and go back to the list, you will update the adapter with all the database entries

Override Back button to save data using SharedPreferences

When using my app the users inputs data and the data is supposed to be saved. However at the moment the back button erases the data from the EditText. Here is my java file for the activity, this activity is activated by a button press from the Main Activity. I've already tried to override the back button with no success. Any suggestions?
public class NewLocation extends ActionBarActivity {
public String coName;
public String coAddress;
public String coContact;
public String sqFt;
public String taxed;
public String concerns;
public EditText editCoName;
public EditText editCoAddress;
public EditText editCoContact;
public EditText editSqFt;
public EditText editTaxed;
public EditText editConcerns;
public static final String DEFAULT = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_location);
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(NewLocation.this, RoomList.class));
}
});
editCoName = (EditText) findViewById(R.id.CoName);
editCoAddress = (EditText) findViewById(R.id.CoAddress);
editCoContact = (EditText) findViewById(R.id.CoContact);
editSqFt = (EditText) findViewById(R.id.SqFt);
editTaxed = (EditText) findViewById(R.id.Taxed);
editConcerns = (EditText) findViewById(R.id.Concerns);
coName = editCoName.getText().toString();
coAddress = editCoAddress.getText().toString();
coContact = editCoContact.getText().toString();
sqFt = editSqFt.getText().toString();
taxed = editTaxed.getText().toString();
concerns = editConcerns.getText().toString();
LoadPreferences();
}
#Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("coName", coName);
outState.putString("coAddress", coAddress);
outState.putString("coContact", coContact);
outState.putString("sqFt", sqFt);
outState.putString("taxed", taxed);
outState.putString("concerns", concerns);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
coName = savedInstanceState.get("coName").toString();
coAddress = savedInstanceState.get("coAddress").toString();
coContact = savedInstanceState.get("coContact").toString();
sqFt = savedInstanceState.get("sqFt").toString();
taxed = savedInstanceState.get("taxed").toString();
concerns = savedInstanceState.get("concerns").toString();
}
private void SavePreferences()
{
SharedPreferences sharedPreferences = getSharedPreferences("New Location Data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("coName", coName);
editor.putString("coAddress", coAddress);
editor.putString("coContact", coContact);
editor.putString("sqFt", sqFt);
editor.putString("taxed", taxed);
editor.putString("concerns", concerns);
editor.apply();
}
private void LoadPreferences()
{
SharedPreferences sharedPreferences = getSharedPreferences("New Location Data", Context.MODE_PRIVATE);
coName = sharedPreferences.getString("coName", DEFAULT);
coAddress = sharedPreferences.getString("coAddress", DEFAULT);
coContact = sharedPreferences.getString("coContact", DEFAULT);
sqFt = sharedPreferences.getString("sqFt", DEFAULT);
taxed = sharedPreferences.getString("taxed", DEFAULT);
concerns = sharedPreferences.getString("concerns", DEFAULT);
editCoName.setText(coName);
editCoAddress.setText(coAddress);
editCoContact.setText(coContact);
editSqFt.setText(sqFt);
editTaxed.setText(taxed);
editConcerns.setText(concerns);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_new_location, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
switch(item.getItemId())
{
case R.id.home:
SavePreferences();
startActivity(new Intent(getApplicationContext(), MainPage.class));
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
SavePreferences();
super.onBackPressed();
}
}
You should save/restore the persistent state of the activity in onSaveInstanceState/onRestoreInstanceState methods. You already override those, so just call SavePreferences/LoadPreferences from them.
Overriding the back button is not a good idea and saving state in onDestroy is also not recommended. Either onSaveInstanceState or onPause is intended for that.
If the data needs to be saved whenever the user leaves that activity, you could approach the problem with a TextWatcher. Override the onTextChanged() method of the TextWatcher with something like this:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length > 0) coName = s;
}
And then override onPause() to save the state of your variables if necessary.
[Edited] You can try:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
SavePreferences();
finish(); //<-- You need finish your activity after saved
return true;
}
return super.onKeyDown(keyCode, event);
}

The SharedPreferences code was no function to make button Visible

First time i had done coding without preferences the button can visible but when i put preferences code the button not visible
i have 3 class it is menu.class, levelone.class, leveltwo.class
this is the following code of menu.class like this
public class menu extends Activity {
Button f1, f2;
ImageView f2lock;
boolean levelTwoUnlocked;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.famouslevel);
f1 =(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level1 = new Intent ();
level1.setClassName ("com.example.game", "com.example.game.levelone");
startActivityForResult (level1, 0);
}
});
}
public void onActivityResult (int requestCode, int resultCode, Intent level1) {
super.onActivityResult (requestCode, resultCode, level1);
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
switch (resultCode) {
case 2: SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();
if(levelTwoUnlocked){
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
else {
f2.setVisibility(View.GONE);
f2lock.setVisibility(View.VISIBLE);
}
}
f2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level2 = new Intent ();
level2.setClassName ("com.example.game", "com.example.game.leveltwo");
startActivityForResult (level2, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splashscreen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and in levelone.class i have put this code
public void onClick(View v){
setResult (2);
finish();
}
});
thats code is to make button visible in menu.class but when levelone.class finish(); is nothing happen with the button, it's still GONE
f2 button function is to open leveltwo.class and in leveltwo.class had same code to set f3 button visible
public void onClick(View v){
setResult (3);
finish();
}
});
and so on with the next level had a same code to make button visible
Did my code setResult is wrong or the preferences code make it not function?
Int the lines:
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();
You just put always false to "f2", because levelTwoUnlocked is always false.
Assign a boolean value to leveTwoUnloacked
or
make changes in
editor.putBoolean("f2",true);
and check
if(prefernces.getBoolean("f2",false))
{
//your code
}

App crashing when saving to shared preferences

I am trying to save to shared preferences. I want to be able to load from shared preferences but when I save and or load at the moment my app crashes.
I also want to be able to have my handler/runnable resume when my app starts up. How can I do this?
Here is my code:
public class MainActivity extends ActionBarActivity {
public void save(){
Editor editor = pref.edit();
editor.putInt("countertest", counter);
editor.commit();
}//end of save
public void read(){
counter = pref.getInt("countertest", counter);
}//end of read
Handler testHandler = new Handler();
int counter;
TextView testView;
Button testButton;
Runnable Runnable0;
SharedPreferences pref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testView = (TextView) findViewById(R.id.testView);
testButton = (Button) this.findViewById(R.id.testButton);
// read();
testView.setText(Integer.toString(counter));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
//read();
final Runnable Runnable0 = new Runnable() {
#Override
public void run() {
counter += 1;
testView.setText(Integer.toString(counter));
testHandler.postDelayed(this, 1000);
// save();
}// end of if
};
/* button click */
testButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Start runnable */
testButton.setEnabled(false);
counter -= 5;
//save();
testView.setText(Integer.toString(counter));
testHandler.post(Runnable0);
}// end of onClick
});// end of blingButton onClickListener
super.onResume();
}//end of onResume
#Override
protected void onPause() {
//save();
testHandler.removeCallbacks(Runnable0);
super.onPause();
}//end of onPause
}
You haven't initialize your pref object. You have just declared it. So you need to do it on onCreate() by
pref = getSharedPreferences("MySP", 0);
OR
pref = PreferenceManager.getDefaultSharedPreferences(this);

Android: Quiz Application using if else statement

If Texboxt = " "
nextActivity.show
else msg = "Sorry!"
Can anyone help and tell me how to make this in Android eclipse java?
I have a code here, but I don't know how to correct this.
private EditText inputtxt;
private Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g1);
inputtxt = (EditText) findViewById(R.id.editText1);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View ContentView) {
// TODO Auto-generated method stub
String name;
name=inputtxt.getText().toString();
if (name.contentEquals("Accounting"))
{
Intent myIntent = new Intent (ContentView.getContext(), NextActivity.class);
startActivityForResult(myIntent, 0);
}
else
{ Toast.makeText(getBaseContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.g1, menu);
return true;
}
`
update your code like following it might work..
private EditText inputtxt;
private Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g1);
inputtxt = (EditText) findViewById(R.id.editText1);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View ContentView) {
// TODO Auto-generated method stub
String name;
name=inputtxt.getText().toString();
if (name.equalsIgnoreCase("Accounting"))
{
Intent myIntent = new Intent (QuizActivity.this, NextActivity.class);
startActivity(myIntent);
}
else
{ Toast.makeText(getApplicationContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.g1, menu);
return true;
}
Compare your string with this
if(name.equalsIgnoreCase("Accounting"))
if(name.equalsIgnoreCase("Accounting"))
instead of
if (name.contentEquals("Accounting"))
Using startActivityForResult(intent) will open a new activity and the previous activity waits for a result from the new activity. Use Bundle to share message between the activities and use startActivity(intent) instead of startActivityForResult(intent)
if (value.equals("")) {
Log.e("value equal to zero", value);
} else {
Log.e(value, "value not equal to zero");
startActivity(new Intent(Registernew.this, next.class));
finish();
}
Change To:
if (name.length()==0)
{
Intent myIntent = new Intent (ContentView.getContext(), NextActivity.class);
startActivityForResult(myIntent, 0);
}
else
{
Toast.makeText(getBaseContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}

Categories

Resources