OnPause() and also onDestroy() never called when other activity opened - java

I have an app with many activities using intent. I have a sharedPreferences method for saving values.
I have a welcome screen that I set to display when the app launches (onCreate is called). In the same activity, I delete the sharePreferences method so when I reopen the app the welcome screen will launch again.
The problem is, when I change to another activity and back again to the main activity and press the exit button. When I launch the app again, it does not show the welcome screen. I think, the main activity is still running so, onPause(), onStop() and onDestroy() are never called. When I stay in the same activity (main) and press exit, it will call onDestroy() (that contains the erase sharedPreferences method).
I didn't set the finish() method. If I set that, every moved activity will call onDestroy in Main activity so the welcome screen will launch every move to that activity.
My mainactivity.java
package com.bani.latihan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class Main extends AppCompatActivity {
RelativeLayout PopupScreen, layoutAsli;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onDestroy(){
super.onDestroy();
SharedPreferences settings = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE);
settings.edit().remove("ditekan").apply();
}
#Override
public void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.main);
Button btn1 =(Button)findViewById(R.id.button1);
final Button btn2 =(Button)findViewById(R.id.button2);
PopupScreen = (RelativeLayout)findViewById(R.id.PopupScreen);
layoutAsli = (RelativeLayout)findViewById(R.id.layoutAsli);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(Main.this, Intent2.class);
startActivity(intent2);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
moveTaskToBack(true);
finish();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Isi dewek cuk!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
EditText tvText = (EditText) findViewById(R.id.editText1);
SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
if (prefs.contains("text")){
tvText .setText(prefs.getString("text", ""));
}
SharedPreferences prefs2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE);
if (prefs2.contains("ditekan")){
PopupScreen.setVisibility(View.INVISIBLE);
layoutAsli.setVisibility(View.VISIBLE);
}
}
public void dismisWelcomeMessageBox(View view) {
PopupScreen.setVisibility(View.INVISIBLE);
layoutAsli.setVisibility(View.VISIBLE);
}
#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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPause() {
super.onPause();
EditText tvText = (EditText) findViewById(R.id.editText1);
Button btWelcome = (Button) findViewById(R.id.button_welcome);
SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit();
SharedPreferences.Editor prefEditor2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE).edit();
prefEditor.putString("text", tvText.getText().toString());
prefEditor2.putBoolean("ditekan", btWelcome.isPressed());
prefEditor.apply();
prefEditor2.apply();
}
}
My another activity code:
package com.bani.latihan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
/**
* Created by Bani Burhanuddin on 21/02/2016.
*/
public class Intent2 extends AppCompatActivity {
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.intent);
Button btnnext = (Button) findViewById(R.id.button5);
Button btnhome = (Button) findViewById(R.id.button6);
Button btnback = (Button) findViewById(R.id.button7);
btnnext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Intent3.class));
finish();
}
});
btnhome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Main.class));
finish();
}
});
btnback.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Main.class));
finish();
}
});
CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1);
final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setVisibility(View.GONE);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
imageView2.setVisibility(View.VISIBLE);
} else {
imageView2.setVisibility(View.GONE);
}
}
});
SharedPreferences prefs = getSharedPreferences("Preferences2", Context.MODE_PRIVATE);
if (prefs.contains("text2")) {
toggle.setChecked(prefs.getBoolean("text2", true));
}
}
#Override
public void onPause() {
super.onPause();
CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1);
if (toggle.isChecked()) {
SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit();
editor.putBoolean("text2", true);
editor.apply();
} else {
SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit();
editor.putBoolean("text2", false);
editor.apply();
}
}
}
My Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.bani.latihan.splashscreen"
android:label="#string/app_name"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.bani.latihan.Main"
android:screenOrientation="sensor"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent2 activity -->
<activity
android:name=".Intent2"
android:screenOrientation="sensor"
android:noHistory="true"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent3 activity -->
<activity
android:name=".Intent3"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent4 activity -->
<activity
android:name=".Intent4"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
</application>
</manifest>
My main layout
<RelativeLayout
android:id="#+id/PopupScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#123456"
android:orientation="vertical"
android:layout_margin="16dp"
android:padding="16dp"
android:visibility="visible">
<TextView
android:id="#+id/welcome_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="#string/welcome"
android:textColor="#ddd333"
android:textSize="28sp"
android:textStyle="bold" />
<TextView
android:id="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome_title"
android:text="#string/welcome_message"
android:textColor="#0dff00"
android:textSize="18sp" />
<Button
android:layout_width="wrap_content"
android:id="#+id/button_welcome"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom"
android:background="#3b978d"
android:onClick="dismisWelcomeMessageBox"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="#string/ok"
android:textColor="#fff" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutAsli"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/hello_world"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:layout_below="#+id/hello_world"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:src="#drawable/imageview"
android:layout_gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button1"
android:layout_marginTop="111dp"
android:layout_centerHorizontal="true"
android:layout_gravity="left|bottom" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:id="#+id/button2"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:layout_gravity="right|bottom" />
</FrameLayout>
</RelativeLayout>
</RelativeLayout>

I got ur problem. Currently u are using the MoveTaskToBack . It will not destroy the activity ,it just move back in ativity stack after that u finish
activity. So its not calling other callback methods. It always call onDestroy
Use either moveTasktoBack or finish. Dont use both, it will confusing.

Related

First activity is only working on my Android project

I am new to android and java programming so i try to learn from doing few projects and i am stuck with this problem.
When i try to run the app only first activity is working and second activity is not working.
Where did i missed on this app?
I am confuse and two java projects are working but the only first activity works.
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android_examples.wallpaper_android_examplescom.MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/img76"
android:layout_above="#+id/btn_right"
android:layout_alignParentTop="true"
android:layout_marginBottom="30dp"
android:scaleType="fitXY" />
<Button
android:id="#+id/btn_right"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_left"
android:layout_alignBottom="#+id/btn_left"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:text="RIGHT" />
<Button
android:id="#+id/btn_left"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="43dp"
android:layout_marginLeft="43dp"
android:layout_marginBottom="100dp"
android:text="LEFT" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:text="Click here to Set this image as Wallpaper in android programmatically" />
</RelativeLayout>
MainActivity.java
package com.android_examples.wallpaper_android_examplescom;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
WallpaperManager wallpaperManager ;
Bitmap bitmap1, bitmap2 ;
DisplayMetrics displayMetrics ;
int width, height;
BitmapDrawable bitmapDrawable ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
bitmap1 = bitmapDrawable.getBitmap();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GetScreenWidthHeight();
SetBitmapSize();
wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try {
wallpaperManager.setBitmap(bitmap2);
wallpaperManager.suggestDesiredDimensions(width, height);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void GetScreenWidthHeight(){
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
public void SetBitmapSize(){
bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);
}
}
login.java
package com.android_examples.wallpaper_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class login extends AppCompatActivity {
private ImageView imageView;
private Button btn_right, btn_left;
private int current_image_index;
private int[] images = {R.drawable.img76, R.drawable.img2};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayImage();
SwitchButton();
}
void DisplayImage(){
imageView = (ImageView)findViewById(R.id.imageView);
}
void SwitchButton(){
btn_right = (Button)findViewById(R.id.btn_right);
btn_left = (Button)findViewById(R.id.btn_left);
btn_right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imageView.setImageResource(images[current_image_index]);
}
});
btn_left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index--;
if(current_image_index < 0){
current_image_index = images.length - 1;
}
imageView.setImageResource(images[current_image_index]);
}
}
);
}
}
Androidmanifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_examples.wallpaper_android_examplescom">
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>
Where ever you want to call the next activity use this:-
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Theres no code to navigate to MainActivity
Trigger below code
private void startMainActivity(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
You have to start your second activity
startActivity(new Intent(getApplicationContext(), MainActivity.class));
make sure that you have added second activity in manifest file
void SwitchButton() {
btn_right = (Button) findViewById(R.id.btn_right);
btn_left = (Button) findViewById(R.id.btn_left);
btn_right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imageView.setImageResource(images[current_image_index]);
startMainPage();
}
});
btn_left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index--;
if (current_image_index < 0) {
current_image_index = images.length - 1;
}
imageView.setImageResource(images[current_image_index]);
startMainPage();
}
}
);
}
private void startMainPage() {
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
}
write this code in your click listener:
btn_right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imageView.setImageResource(images[current_image_index]);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});

Android - Store variable and use in webview link

I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.
Here's what I have so far;
LoginActivity.java
package com.example.app;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class LoginActivity extends Activity {
EditText subdomain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subdomain = (EditText) findViewById(R.id.subdomain);
Button btn=(Button)findViewById(R.id.sign_in_button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(LoginActivity.this,
MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
public void saveInfo (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", YourSchool.getText().toString());
editor.commit();
}
}
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a
browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");
// Stop local links and redirects from opening in browser instead
of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void displayData (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
String client_subdomain = sharedPref.getString("name", "");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_login.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:gravity="center_horizontal"
android:orientation="vertical"
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.example.app.LoginActivity">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/YourSchool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your school"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="webView"
android:text="SIGN IN"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
activity_main.xml
<RelativeLayout
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"
tools:context=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="#string/app_name"></activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain
I think it's probably something small that I've missed out but any help would be greatly appreciated.
Many thanks,
Sam
Store the variable using sharedPrefeence as below,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("your_string_key", yourStringValue);
editor.commit();
Then retrieve the value as,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
String myStringValue = sp.getInt("your_string_key", -1);
Use the variable in the loadUrl as
mWebView.loadUrl("https://"+myStringValue+".mydomain.co.uk");

Android: StoredPreferences + WebView Link

I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.
Here's what I have so far;
LoginActivity.java
package com.example.app;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class LoginActivity extends Activity {
EditText subdomain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subdomain = (EditText) findViewById(R.id.subdomain);
Button btn=(Button)findViewById(R.id.sign_in_button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(LoginActivity.this,
MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
public void saveInfo (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", YourSchool.getText().toString());
editor.commit();
}
}
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a
browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");
// Stop local links and redirects from opening in browser instead
of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void displayData (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
String client_subdomain = sharedPref.getString("name", "");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_login.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:gravity="center_horizontal"
android:orientation="vertical"
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.example.app.LoginActivity">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/YourSchool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your school"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="webView"
android:text="SIGN IN"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
activity_main.xml
<RelativeLayout
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"
tools:context=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="#string/app_name"></activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain
I think it's probably something small that I've missed out but any help would be greatly appreciated.
Many thanks,
Sam
Its because haven't declared "client_subdomain" as a variable within scope of onCreate
why don't you try changing displayData to
public String displayData () {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
return sharedPref.getString("name", "");
}
and in onCreate of the Main Activity
...
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+displayData()+".domain.co.uk/texts");
...
Also, I don't see you ever calling "saveInfo" anywhere in your Login Activity. As well, you should probably change the function too. You aren't grabbing the text from the EditText correctly (whatever YourSchool) is
public void saveInfo() {
SharedPreferences sharedPref = getSharedPreferences("spfile", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", subdomain.getText().toString());
editor.commit();
}
And this will be what your onCreate should have,
protected void onCreate(Bundle savedInstanceState) {
...
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
saveInfo();
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
...
}

xml layout crashes on loading for android app

enter image description hereHey guys I'm at the end of my Java 1 class working on my project. We are making a memory/concentration game. My issue is that when I click the easy button for the next activity the app crashes. I have tried using fragments and activities and just can't seem to get it right. I have also tried using the layout I need on my main activity just to see if I could get it to display. Even then it just crashes on startup of the app. Any help would be appreciated.
Startup Screen activity.
package com.bignerdranch.android.memory;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MemoryActivity extends Activity {
private Button mEasy;
private Button mMedium;
private Button mHard;
private CheckBox mSilence;
public MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memory);
player = new MediaPlayer();
player = MediaPlayer.create(this, R.raw.mkstartmusic);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setLooping(true);
player.start();
mEasy = (Button)findViewById(R.id.easy);
mEasy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent easy = new Intent(getApplicationContext(), EasyGame.class);
startActivity(easy);
}
});
mMedium = (Button)findViewById(R.id.medium);
mMedium.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
mHard = (Button)findViewById(R.id.hard);
mHard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
mSilence = (CheckBox)findViewById(R.id.silence);
mSilence.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mSilence.isChecked()) {
player.pause();
} else if(mSilence.isChecked() == false) {
player.start();
}
}
});
}
#Override
protected void onStop() {
super.onPause();
if (player != null){
player.stop();
if (isFinishing()){
player.stop();
player.release();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.activity_memory, menu);
return true;
}
}
Second Activity (Easy option)
package com.bignerdranch.android.memory;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
public class EasyGame extends Activity {
private ImageButton buttOne;
private ImageButton buttTwo;
private ImageButton buttThree;
private ImageButton buttFour;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy);
buttOne = (ImageButton)findViewById(R.id.ImageButton01);
buttOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
buttTwo = (ImageButton)findViewById(R.id.ImageButton02);
buttTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
buttThree = (ImageButton)findViewById(R.id.ImageButton03);
buttThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
buttFour = (ImageButton)findViewById(R.id.ImageButton04);
buttFour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.activity_easy, menu);
return true;
}
}
This is the layout for the Easy option
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/easyback"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/easyback"
android:clickable="false"
android:duplicateParentState="false"
android:longClickable="false"
android:scaleType="centerCrop" />
<ImageButton
android:id="#+id/ImageButton04"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="#+id/ImageButton01"
android:layout_toRightOf="#+id/ImageButton01"
android:layout_toEndOf="#+id/ImageButton01"
android:maxHeight="25dp"
android:maxWidth="25dp"
android:scaleType="fitXY"
android:src="#drawable/dragonemb" />
<ImageButton
android:id="#+id/ImageButton02"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_above="#+id/ImageButton04"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="22dp"
android:layout_marginEnd="22dp"
android:maxHeight="25dp"
android:maxWidth="25dp"
android:scaleType="fitXY"
android:src="#drawable/dragonemb" />
<ImageButton
android:id="#+id/ImageButton03"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignTop="#+id/ImageButton04"
android:layout_toLeftOf="#+id/ImageButton04"
android:layout_toStartOf="#+id/ImageButton04"
android:maxHeight="25dp"
android:maxWidth="25dp"
android:scaleType="fitXY"
android:src="#drawable/dragonemb" />
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_marginTop="152dp"
android:layout_toLeftOf="#+id/ImageButton02"
android:maxHeight="25dp"
android:maxWidth="25dp"
android:scaleType="fitXY"
android:src="#drawable/dragonemb" />
</RelativeLayout>
OK It's not the full crash log you posted but at the top of it I saw roidManifest.xml?. And It's sure that you didn't defined your EasyGame Activity in your androidmanifest.xml so add this line inside application tag,
<manifest package="com....." . . . >
<application . . . >
<activity
android:name=".EasyGame"
android:label="easygame">
</activity>
. . .
</application>
</manifest>
In addition you are trying to cast your ImageButton into Button consider fixing that as well.
Add code below to AndroidManfest
<activity
android:name=".EasyGame"
/>
its simple just add this line to your AndroidManifest
<activity android:name="Activity"/>
The logcat did mentioned that you need to declare your activity within the Android Manifest which you didn't. please read the logcat carefully as it really helps to find what went wrong.
OK, so I had tried all of your guys suggestions which I had were part of the issue, the final issue turned out to that I needed to add my images to the drawable-xhdpi. Thanks for all your help.

how to move from my MyAcctivity.java (login page) to HomeActivity.java (home page) with a click of button- INTELLIJ

It doesnt have any errors ..when i run on my emulator it UNFORTUNATELY STOPS
i am trying to make my button when i click it takes me to the next page....kindly help!
Here is codes for MyActivity.java
package com.example.INIKO_EVENTS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
Button login;
Button sign_up;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login=(Button)findViewById(R.id.Button15);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this,HomeActivity.class);
MyActivity.this.
startActivity(i);
}
});
sign_up=(Button)findViewById(R.id.signUpButton);
sign_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this,SignUpActivity.class);
MyActivity.this.
startActivity(i);
}
});
}
}
and heres my HomeActivity.java - it has buttons linking to other pages
package com.example.INIKO_EVENTS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HomeActivity extends Activity {
/**
* Created by eddie kamau on 2/14/14.
*/
Button button2;
Button button3;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
button2 = (Button)findViewById(R.id.toEventButton);
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,EventActivity.class);
HomeActivity.this.
startActivity(i);
}
});
button3=(Button)findViewById(R.id.manage_your_guestButton2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,GuestActivity.class);
HomeActivity.this.
startActivity(i);
}
});
}
}
main.xml
<Button
android:layout_width="141dp"
android:layout_height="wrap_content"
android:text="#string/login"
android:layout_margin="10sp"
android:id="#+id/Button15"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:onClick="onClick"/>
<Button
android:layout_width="121dp"
android:layout_height="wrap_content"
android:text="#string/sign_up"
android:id="#+id/signUpButton"
android:layout_gravity="center_horizontal"
android:maxWidth="600dp"
android:clickable="true"
android:onClick="onClick"/>
</LinearLayout>
home.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create_your_event"
android:id="#+id/toEventButton"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:onClick="onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/manage_your_event"
android:id="#+id/manage_your_guestButton2"
android:layout_gravity="center_horizontal" android:clickable="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/your_events"
android:id="#+id/your_eventsButton3"
android:layout_gravity="center_horizontal" android:clickable="true"/>
</LinearLayout>
try this
mainactivity.java
public class MainActivity extends Activity {
Button login;
Button sign_up;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login=(Button)findViewById(R.id.button1);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,HomeActivity.class);
startActivity(i);
}
});
sign_up=(Button)findViewById(R.id.button2);
sign_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,HomeActivity.class);
startActivity(i);
}
});
}
}
homeactivity.java
public class HomeActivity extends Activity{
Button button2;
Button button3;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,EventActivity.class);
HomeActivity.this.
startActivity(i);
}
});
button3=(Button)findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(HomeActivity.this,GuestActivity.class);
HomeActivity.this.
startActivity(i);
}
});
}
}
manifest.xml
define all this class in mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.target"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.target.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.target.EventActivity" >
</activity>
<activity android:name="com.example.target.GuestActivity" >
</activity>
<activity android:name="com.example.target.HomeActivity" >
</activity>
</application>

Categories