forked from turbolinks/turbolinks-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
116 lines (91 loc) · 3.9 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.basecamp.turbolinks.demo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.basecamp.turbolinks.TurbolinksSession;
import com.basecamp.turbolinks.TurbolinksAdapter;
import com.basecamp.turbolinks.TurbolinksView;
public class MainActivity extends AppCompatActivity implements TurbolinksAdapter {
// Change the BASE_URL to an address that your VM or device can hit.
private static final String BASE_URL = "http://10.0.1.100:9292";
private static final String INTENT_URL = "intentUrl";
private String location;
private TurbolinksView turbolinksView;
// -----------------------------------------------------------------------
// Activity overrides
// -----------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the custom TurbolinksView object in your layout
turbolinksView = (TurbolinksView) findViewById(R.id.turbolinks_view);
// For this demo app, we force debug logging on. You will only want to do
// this for debug builds of your app (it is off by default)
TurbolinksSession.getDefault(this).setDebugLoggingEnabled(true);
// For this example we set a default location, unless one is passed in through an intent
location = getIntent().getStringExtra(INTENT_URL) != null ? getIntent().getStringExtra(INTENT_URL) : BASE_URL;
// Execute the visit
TurbolinksSession.getDefault(this)
.activity(this)
.adapter(this)
.view(turbolinksView)
.visit(location);
}
@Override
protected void onRestart() {
super.onRestart();
// Since the webView is shared between activities, we need to tell Turbolinks
// to load the location from the previous activity upon restarting
TurbolinksSession.getDefault(this)
.activity(this)
.adapter(this)
.restoreWithCachedSnapshot(true)
.view(turbolinksView)
.visit(location);
}
// -----------------------------------------------------------------------
// TurbolinksAdapter interface
// -----------------------------------------------------------------------
@Override
public void onPageFinished() {
}
@Override
public void onReceivedError(int errorCode) {
handleError(errorCode);
}
@Override
public void pageInvalidated() {
}
@Override
public void requestFailedWithStatusCode(int statusCode) {
handleError(statusCode);
}
@Override
public void visitCompleted() {
}
// The starting point for any href clicked inside a Turbolinks enabled site. In a simple case
// you can just open another activity, or in more complex cases, this would be a good spot for
// routing logic to take you to the right place within your app.
@Override
public void visitProposedToLocationWithAction(String location, String action) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(INTENT_URL, location);
this.startActivity(intent);
}
// -----------------------------------------------------------------------
// Private
// -----------------------------------------------------------------------
// Simply forwards to an error page, but you could alternatively show your own native screen
// or do whatever other kind of error handling you want.
private void handleError(int code) {
if (code == 404) {
TurbolinksSession.getDefault(this)
.activity(this)
.adapter(this)
.restoreWithCachedSnapshot(false)
.view(turbolinksView)
.visit(BASE_URL + "/error");
}
}
}